Board index » delphi » Sample code in Help for Threads is not OK

Sample code in Help for Threads is not OK

I think i found an error in the Help files, on waiting for a thread to
finish (D4, no updates)

Help/Index/ select 'Threads waiting', and then the first item 'Waiting for a
task to be completed'.

The sample code mentions the use of a TEvent in the OnTerminate of the
thread to signal the main thread that is has finished. But the OnTerminate
is executed in the context of the main thread, and the code which is waiting
for the event to be set (which is done in the OnTerminate) is also running
in the main thread. In the sample code this waiting for the event to be set
is done just after creating and starting the threads. This does not work.

In one of my programs i use another way of waiting for threads to finish
before continuing execution in the main thread. I simply use a counter of
the number of threads still running. In the OnTerminate i decrement this
counter. In the waiting-for-all-threads-to-be-finished code i put

...
{ Wait for all threads to be finished }
while NumberOfThreads > 0 do
  Application.ProcessMessages
...

This does work, although personally i do not like this solution very much.
Does anybody know a more elegant way?

TThread.WaitFor is not a solution, because then Synchronize cannot be used
in the threaded code.

Koen

 

Re:Sample code in Help for Threads is not OK


Koen van Dijken (k.van.dij...@wxs.nl) wrote:
: I think i found an error in the Help files, on waiting for a thread to
: finish (D4, no updates)

: Help/Index/ select 'Threads waiting', and then the first item 'Waiting for a
: task to be completed'.
<snip>
: This does work, although personally i do not like this solution very much.
: Does anybody know a more elegant way?

There is an article in the latest Delphi Informant magazine
(November, I believe) which discusses the problem and proposes
a solution using Windows API calls.  For free downloadable
source code and subscription/back issue ordering information,
http://www.informant.com/delphi/
--
============================================================
Rick Carter   carte...@email.uc.edu   rcar...@tso.cin.ix.net
============================================================

Other Threads