RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TConditionVariableCS.WaitFor Method (TCriticalSection, LongWord)

Sleeps on the condition variable and releases the given critical section.

Pascal
function WaitFor(CriticalSection: TCriticalSection; TimeOut: LongWord = INFINITE): TWaitResult; overload;
function WaitFor(var CriticalSection: TRTLCriticalSection; TimeOut: LongWord = INFINITE): TWaitResult; overload;
C++
__fastcall TWaitResult WaitFor(TCriticalSection CriticalSection, LongWord TimeOut = INFINITE);
__fastcall TWaitResult WaitFor(TRTLCriticalSection CriticalSection, LongWord TimeOut = INFINITE);

Call WaitFor to sleep on the condition variable and to release the critical section specified through the CriticalSection parameter.  

The TimeOut parameter sets the time-out interval. When this interval elapses, the critical section is reacquired, even if the condition variable has not been awakened by other threads.  

The result returned by the WaitFor function is of TWaitResult type and can have one of the following values:

Value 
Meaning 
wrSignaled  
The condition variable has been awakened.  
wrTimeout  
The time-out interval has elapsed without the condition variable being awakened.  
wrAbandoned  
The condition variable has been destroyed before the time-out interval has elapsed.  
wrError  
An error occured while waiting. Check the LastError property for an error code giving more information.  

Condition variables might suddenly wake up, without making an explicit call to the Release or ReleaseAll methods. Also, other threads might manage to run before the thread that sleeps on the condition variable is awakened. Therefore, make the call to WaitFor inside a loop. To avoid the above mentioned errors, the loop should test that the desired condition becomes true even after the condition variable is awakened. 

 

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