Checking for Termination by Other Threads

From RAD Studio
Jump to: navigation, search

Go Up to Writing the Thread Function

Your thread object begins running when the Execute method is called (see Executing thread objects) and continues until Execute finishes. This reflects the model that the thread performs a specific task, and then stops when it is finished. Sometimes, however, an application needs a thread to execute until some external criterion is satisfied.

You can allow other threads to signal that it is time for your thread to finish executing by checking the Terminated property. When another thread tries to terminate your thread, it calls the Terminate method. Terminate sets your thread's Terminated property to True. It is up to your Execute method to implement the Terminate method by checking and responding to the Terminated property. The following example shows one way to do this:

procedure TMyThread.Execute;
  while not Terminated do
void __fastcall TMyThread::Execute() {
    while (!Terminated)

See Also