[DUG] Stopping threads in applications

Kyley Harris kyleyharris at gmail.com
Fri Feb 1 16:31:02 NZDT 2008


Well, Two options..

1. write very granular threaded code, and dont call procedures that are not
also granular and make sure they all support a cancel callback. when writing
threads, I tend to only use them in a way that allows me to finish it in a
timely fashion.

eg


while not terminated do
begin
  do500things(cancelcallback)
end;

function cancelcallback:boolean;
begin
  result := terminated;
end;

procedure do500things
begin
  for x := 1 to 500 do
    if CancelCallback then exit else dosomethingsmall
end;


if you really cant make it granular, then set the thread to do a
freeonterminate. when you kill the thread, dont use free, or waitfor..
instead keep some state tracking and call terminate from your cancel button,
you can handle cleanup from the onterminate event later.

On Feb 1, 2008 2:52 PM, Phil Scadden <p.scadden at gns.cri.nz> wrote:

>
> > I have been using threads in applications for a while now, and one
> > thing that always seems back to haunt me is stopping the thread.
> > When using TThread you can call the Terminate procedure which simply
> > sets the FTerminated variable of the thread to True, which you then
> > have to check for in  your thread like
> >
> What about just calling the SDK TerminateThread using the thread handle?
>
> --
> Phil Scadden, Senior Scientist GNS Science Ltd 764 Cumberland St,
> Private Bag 1930, Dunedin, New Zealand Ph +64 3 4799663, fax +64 3 477
> 5232
>
> Notice: This email and any attachments are confidential. If received in
> error please destroy and immediately notify us. Do not copy or disclose the
> contents.
>
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at listserver.123.net.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at listserver.123.net.nz with
> Subject: unsubscribe
>



-- 
Kyley Harris
Harris Software
+64-21-671-821
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserver.123.net.nz/pipermail/delphi/attachments/20080201/eca24490/attachment.html


More information about the Delphi mailing list