I've had just another idea: Is it ok to set FreeOnTerminate:=false and free the thread within the OnTerminate event?
No, you can't free anything in its own event.
Pity, because it worked flawlessly.
On the other hand, if enable
FreeOnTerminate in the
OnTerminate event handler, I get a memory leak. Which irritates me because the
topic mentioned afore says that the
OnTerminate handler is called first, then
FreeOnTerminate is checked. So it should be possible to activate
FreeOnTerminate in the event handler, shouldn't it?
The WaitFor technique should just work but there is something else wrong.
I haven't overlooked that, but it still can't figure out what might go wrong. That's why I'd like to have a best-practice-advice first. I have followed the advice now not to use
FreeOnTerminate. That's ok so far. But I have two situations:
a) The program might want to disconnect from the server, so the http communication thread should terminate. In this case, I don't want to use
WaitFor, but just let the thread finish in the background. But I have to free the thread object somewhen later. That's why I asked whether I can do that in the
OnTerminate.
b) The program is about to exit. Then I *must* wait for the thread to have it terminate gracefully.