RAD Studio |  |

Waiting for a Thread to Finish Executing
See Also
Collapse All

To wait for another thread to finish executing, use the WaitFor method of that other thread. WaitFor doesn't return until the other thread terminates, either by finishing its own Execute method or by terminating due to an exception. For example, the following code waits until another thread fills a thread list object before accessing the objects in the list:

Copy Code

    if ListFillingThread.WaitFor then
    begin
    with ThreadList1.LockList do
    begin
    for I := 0 to Count - 1 do
    ProcessItem(Items[I]);
    end;
    ThreadList1.UnlockList;
    end;

Copy Code

    if (pListFillingThread->WaitFor())
    {
      TList *pList = ThreadList1->LockList();
    for (int i = 0; i < pList->Count; i++)
    ProcessItem(pList->Items[i]);
    ThreadList1->UnlockList();
    }

In the previous example, the list items were only accessed when the WaitFor method indicated that the list was successfully filled. This return value must be assigned by the Execute method of the thread that was waited for. However, because threads that call WaitFor want to know the result of thread execution, not code that calls Execute, the Execute method does not return any value. Instead, the Execute method sets the ReturnValue property. ReturnValue is then returned by the WaitFor method when it is called by other threads. Return values are integers. Your application determines their meaning.

See Also

Waiting for a Task to Be Completed

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