[DUG] Terminating a thread
Conor Boyd
Conor.Boyd at trimble.co.nz
Thu Sep 21 12:01:16 NZST 2006
You're nearly there, I think.
TThread has a Terminated property already.
When you call the existing Terminate method on TThread, it simply sets
the Terminated property to True.
It's up to you to check the value of Terminated while your thread is
processing.
E.g.
Procedure TFetchData.Execute;
begin
while not Terminated do
try
DoSomeStuff;
if Terminated then
Exit;
DoSomeMoreStuff;
if Terminated then
Exit;
DoEvenMoreStuff;
catch
on E: Exception do
// Never let exceptions percolate out of your thread.
end
end;
This sort of thing works fine for me.
HTH,
Conor
-----Original Message-----
From: delphi-bounces at ns3.123.co.nz [mailto:delphi-bounces at ns3.123.co.nz]
On Behalf Of Nick Fauchelle
Sent: Thursday, 21 September 2006 11:43 a.m.
To: NZ Borland Developers Group - Delphi List
Subject: [DUG] Terminating a thread
Hey guys,
I got a thread here (TFetchData = class(TThread)) which downloads a
bunch of xml and then process's it and sticks it into a database.
All working well, however I want the user to be able to abort/terminate
the process...
So one think I did is setup a global varaible - gbTerm : boolean; And in
my thread.execute I would do this If gbTerm = true then terminate;
However that didn't quite work as expected (ie, nothing happened, thread
still ran).
So then I tried
var
MyThread : TFetchData;
begin
MyThread := TFetchData.create('myname'); end;
button2
begin
MyThread.Terminate;
end;
same result as above (to be expected ofc).
So im just wondering... how should I terminate this thread?
Thanks
Nick
_______________________________________________
Delphi mailing list
Delphi at ns3.123.co.nz
http://ns3.123.co.nz/mailman/listinfo/delphi
More information about the Delphi
mailing list