I'm planning to have multithreading in my app but since i started studying TThread class i got some questions:
1. i create an addtl thread from the main one just placing:
MyThread := TMyThread.Create(True);
MyThread.Resume;
But, does the 'MyThread' tread destroy itself after it completes MyThread.Execute method?
2. How can the main thread find weather the MyThread is executed or still in process?
Thank you,
Andrew
> 1. i create an addtl thread from the main one just placing: MyThread :=
> TMyThread.Create(True); MyThread.Resume; But, does the 'MyThread' tread destroy itself
> after it completes MyThread.Execute method?
There is a FreeOnTerminate property - previous delphi versions had a bug
in the implementation of this property iirc. It might be better to use the
event, see below.
> 2. How can the main thread find weather the MyThread is executed or still in process?
Property Suspended and event OnDestroy help you out here.
Werner
You can always do this check:
ThreadFinished := WaitForSingleObject(MyThread.Handle, 0) =
WAIT_OBJECT_0;
Now, if the thread is FreeOnTerminate you obviously cannot hold a
variable to it, because it will become invalid when the thread
finishes and destroys itself.
To overcome this you can use DuplicateHandle() to make a copy of the
thread's handle *before you resume it* and do the wait on the copy
instead. Don't forget to close the handle when you're done with it.
Well, since TThread is not a TComponent descendent you are technically in the wrong group.
b.p.delphi.rtl would have been better.
>
> I'm planning to have multithreading in my app but since i started studying TThread class i got some questions:
> 1. i create an addtl thread from the main one just placing:
> MyThread := TMyThread.Create(True);
> MyThread.Resume;
> But, does the 'MyThread' tread destroy itself after it completes MyThread.Execute method?
There are two parts to consider here. The first is the TMyThread object. It occupies
some memory after you have created it, and that will *not* be automatically
released when the thread has terminated, unless you set FreeOnTerminate = true
before you resume the thread. The second part is the API thread represented by
the objects Handle property. This API thread will have stopped executing when the
code flow has fallen out of the Execute method, but not all of the resources it
has used (stack memory, for instance) will have been returned to the system pool
yet. The thread handle is a Windows kernel object, which, like other kernel objects
like processes has a reference count. The count is not yet zero, so you can still
use the thread objects Handle property with API function like GetExitCodeThread
(the best way to figure out if the thread is still executing, by the way). Only
when CloseHandle is called on the thread objects handle (the thread objects
destructor does that) will the reference count drop to zero and the associated
system resources will be returned to the system pool.
> 2. How can the main thread find weather the MyThread is executed or still in process?
You can only do that if you have not set FreeOnterminate to true, otherwise the
thread objects reference may become invalid at any time, so it is dangerous to
access it after the thread has been started. You do a check like
if GetExitcodeThread( mythread.handle ) = STLL_ACTIVE Then
... thread is not terminated
--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be