Reputation: 2471
I was reading this post that almost solved my problem, since nobody answered my comments I decided to make a question:
Original Post that almost answer my question
As I asked there:
OnTerminate
Event, is also possible they finish at the same time?OnTerminate
method will be "enqueued" by the O.S? In other words, will the code be free from reentrancy, if uses MainThread
properties?Upvotes: 1
Views: 1645
Reputation: 24907
OnTerminate is an event fired on the VCL thread, so it will have been PostMessaged or, more likely, SendMessaged. Either way, the OnTerminates will be serialized.
That said, I have never used this event since D3, (when I found out how much of Delphi thread support actually 'worked').
Edit - you can maybe get your OnTerminate calls to reenter by calling Application.ProcessMessages within it, (if you're feeling particularly suicidal:).
Upvotes: 3
Reputation: 598174
It is possible Multiple threads share the same OnTerminate Event
Yes, just as the answer to the other question showed you.
and finish at the same time?
The threads may finish their work at the same time, but by default the OnTerminate
event handler(s) will not be called at the same time. This is because the OnTerminate
event handler is triggered by TThread
using an internal call to TThread.Synchronize()
, so multiple threads triggering their OnTerminate
events at the same time will not overlap each other. To change that behavior (which most people do not do), you would have to override the virtual TThread.DoTerminate()
method to manually call the OnTerminate
event handler directly without calling TThread.Synchronize()
first.
Upvotes: 5