TThread.FreeOnTerminate property
Indicates whether the thread should free itself when it stops executing.
Declaration
property FreeOnTerminate: Boolean read FFreeOnTerminate write FFreeOnTerminate;Description
FreeOnTerminate, when set to
True indicates that the thread instance will freed automatically as soon as the thread stops executing.
You can use the
OnTerminate property to get a notification of when the thread has terminated and will be freed.
When setting this property to True, in general you may not read or write any property of the TThread instance from a different thread,
because there is no guarantee that the thread instance still exists in memory. This implies 2 things:
- The
OnTerminateevent handler should be set before settingFreeOnTerminatetoTrue - The properties can still be read and set in the
OnTerminateevent handler, as the thread instance is then still guaranteed to exist.
If FreeOnTerminate is set to False, to stop and delete a running thread from another thread, the following sample code can be used:
aThread.Terminate; aThread.WaitFor; FreeAndNil(aThread);
See also
An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.
