RAD Studio |  |

Checking for Termination by Other Threads
See Also
Collapse All

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:

Copy Code

    procedure TMyThread.Execute;
    begin
    while not Terminated do
    PerformSomeTask;
    end;

Copy Code

    void __fastcall TMyThread::Execute()
    {
    while (!Terminated)
    PerformSomeTask();
    }

See Also

Using the Main VCL Thread

Waiting for Other Threads

Using Thread-local Variables

Handling Exceptions in the Thread Function

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!