Question

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:

  • Since it's possible Multiple threads share the same OnTerminate Event, is also possible they finish at the same time?
  • If so, what happens? The call to OnTerminate method will be "enqueued" by the O.S? In other words, will the code be free from reentrancy, if uses MainThread properties?
Was it helpful?

Solution

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.

OTHER TIPS

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:).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top