Waiting for a Thread to Finish Executing

From RAD Studio
Jump to: navigation, search

Go Up to Waiting for Other Threads

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:

if ListFillingThread.WaitFor then
  with ThreadList1.LockList do
    for I := 0 to Count - 1 do
if (pListFillingThread->WaitFor()) {
	TList *pList = ThreadList1->LockList();
	for (int i = 0; i < pList->Count; i++)

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