Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TThread.Terminated

0 views
Skip to first unread message

Christof Binder

unread,
Aug 24, 2000, 3:00:00 AM8/24/00
to
Hello,

Delphi-help and books tell me, that a thread is terminated when its execute
method is done. I conclude that such a thread will set its property
"terminated" to true, but that doesn't seem to happen.
Dummy example:

procedure TForm1.FormCreate(Sender: TObject);
begin
t:=Thread.Create(false);
end;

procedure Thread.Execute;
begin
FreeOnTerminate:=true;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
// t.Terminate;
if t.Terminated then Color:=clRed
else Color:=clGreen;
end;

When I click on Button1 Form1 is green. If I comment "// t.Terminate" out
then of course the thread is terminated but in my "real" application I
cannot do this, the thread itself should know when its finished.

thanx,
Chris

AlanGLLoyd

unread,
Aug 25, 2000, 3:00:00 AM8/25/00
to
In article <8o2q4v$i4e$1...@news.cs.tu-berlin.de>, "Christof Binder"
<bin...@cs.tu-berlin.de> writes:

>I conclude that such a thread will set its property
>"terminated" to true,

Delphi (3) help says :-

"The Terminated property indicates that the thread has been asked to
terminate."

... and advises that the Execute method should check this property and exit
when it is true. Hence it does not tell you that it _has_ terminated, only that
it should.

Use the OnTerminate event to do what you want.

Alan Lloyd
alang...@aol.com

Martin Harvey

unread,
Aug 26, 2000, 3:00:00 AM8/26/00
to
Christof Binder wrote:
>
> Hello,
>
> Delphi-help and books tell me, that a thread is terminated when its execute
> method is done.

Ermm .. yes and no!

A thread may have finished when its execute method is done, but strictly
speaking, the terminate property is true when something has called its
terminate method.

> I conclude that such a thread will set its property

> "terminated" to true, but that doesn't seem to happen.

Correct ... this would only happen if the last line in TMyThread.Execute
said something like "Terminate", or more explicitly "Self.Terminate".

> procedure TForm1.Button1Click(Sender: TObject);
> begin
> // t.Terminate;
> if t.Terminated then Color:=clRed
> else Color:=clGreen;
> end;
>
> When I click on Button1 Form1 is green. If I comment "// t.Terminate" out
> then of course the thread is terminated but in my "real" application I
> cannot do this, the thread itself should know when its finished.

The best way to do this is to use the "OnTerminate" event of the thread,
which (slightly confusingly) is *not* fired when TThread.Terminate is
called, but it fired when the thread *actually* finishes. Try
something like:

{In object inspector or code do ..}
TForm1.HandleThreadTermination = MyThread.OnTerminate;

procedure TForm1.HandleThreadTermination(Sender:TObject);
begin
ASSERT(Sender is TThread);
if (Sender as TThread).Terminated then
begin
Color := clRed;
ShowMessage("Thread was force quit.");
end
else
begin
Color := clGreen;
ShowMessage("Thread finished execution normally.");
end;
end;

Hope this helps. For more info about thread termination see:

http://www.pergolesi.demon.co.uk/prog/threads/ToC.html

MCH.

--
Martin Harvey. mar...@pergolesi.demon.co.uk
http://www.pergolesi.demon.co.uk
ICQ: 37298917

0 new messages