[fpc-pascal] TThread.FreeOnTerminate
Yuriy Sydorov
jura at cp-lab.com
Thu Dec 13 19:52:20 CET 2018
On 12/13/2018 1:06 PM, Martin wrote:
> Is there a way to use FreeOnTerminate other that setting it in the constructor (or before the thread starts / or in the
> rather complex manner below)?
>
> The doc does not mention any limitations https://www.freepascal.org/docs-html/rtl/classes/tthread.freeonterminate.html
>
> However, setting FreeOnTerminate*after* Execute() has finished has no effect.
The question is why you need to set FreeOnTerminate after starting a thread?
FreeOnTerminate is designed for threads which you start and forget about them because accessing the thread object is
dangerous for such threads.
If you need to access the thread object or control the thread's lifetime never use FreeOnTerminate.
To stop such thread use the code like this:
t.Terminate;
tm:=GetTickCount64;
while not t.Finished do
begin
sleep(10);
// Check for timeout, the thread does not respond
if GetTickCount64 - tm > 10000 then
begin
// Do something such as try to kill the thread
break;
end;
end;
if t.Finished then
t.Free;
--
Yuriy.
More information about the fpc-pascal
mailing list