Newsgroups : Borland : borland.public.delphi.rtl.win32 : 2005 Mar : TThread.Synchronize does nothing
| Subject: | TThread.Synchronize does nothing |
| Posted by: | "Zbigniew Lipinski" (info@removethis.firmazl.pl) |
| Date: | Tue, 29 Mar 2005 22:37:47 |
Perhaps this has already been corrected, as I'm still using D4, but:
Suppose you create two threads, immediately one by one:
ThreadA does some short job and ends quickly. The application waits
for its termination.
ThreadB lasts for the whole lifetime of the app and its Execute
method uses Synchronize.
So, in the application there is a sequence of instrustoins:
ThreadA.MyCreate.WaitFor; {MyCreate Sets FreeOnTerminate to true}
BackgrndThread:=ThreadB.Create(false);
In this case, the method passed by ThreadB to its Synchronize method never
gets called. There is no error message, no exception generated. The thread
runs, but does nothing.
The mechanism of the error is following:
At the moment when thread A ends it is the only thread created so far,
so the RemoveThread procedure in the Classes unit posts a CM_DESTROYWINDOW
message.
Normally this should result in execution of the FreeThreadWindow procedure,
however before the message is processed we create another thread.
This thread doesnt create ThreadWindow as it already exists.
Only then the previously posted CM_DESTROYWINDOW message gets processed
and the ThreadWindow is destroyed despite of the fact that another
thread (ThreadB) needs it.
Of course, if we put Application.ProcessMessages betwen the two
statements above, the error will not appear, nevertheless this
behaviour is potentially dangereous because if ThreadB does some
background tasks which don't have immediate visual effects, it's
easy to miss the fact, that ThreadB doesn't do what it should
(as it happened in my case in an application related to security).
So, I think this should be corrected or at least some warning
should be placed in a help for TThread.
I placed a sample application which isolates the problem at the address:
www.firmazl.pl/demos/threaderror.zip
Regards
--
Zbigniew Lipinski
Uwaga, zmiana adesów na:
info@RemoveThis.firmazl.pl
http://www.firmazl.pl