Waits for notification from an object which called the Pulse or PulseAll method or for a specified timer to elapse. If exitContext is true then the synchronization domain for the context (if in a synchronized context) is exited before the wait and reacquired.
[Visual Basic] Overloads Public Shared Function Wait( _ ByVal obj As Object, _ ByVal timeout As TimeSpan, _ ByVal exitContext As Boolean _ ) As Boolean [C#] public static bool Wait( object obj, TimeSpan timeout, bool exitContext ); [C++] public: static bool Wait( Object* obj, TimeSpan timeout, bool exitContext ); [JScript] public static function Wait( obj : Object, timeout : TimeSpan, exitContext : Boolean ) : Boolean;
True if the wait succeeded or did not time out; otherwise, false.
Exception Type | Condition |
---|---|
ArgumentNullException | If obj is a null reference (in Visual Basic Nothing). |
ArgumentException | If timeout is negative or greater than MaxValue. |
SynchronizationLockException | Wait is not invoked from within a synchronized block of code. |
ThreadInterruptedException | ThreadInterruptedException if the thread that invokes Wait is later interrupted from the waiting state. This happens when another thread calls this thread's Interrupt method. |
The thread that currently holds the lock on this object invokes this method in order to wait until a condition in the object's state has been met. Shortly after the call to Wait, the thread that invoked Wait releases the lock, enters the waiting queue, and the next thread in the ready queue (if there is one) is allowed to take control of the lock. The thread that invoked Wait remains in the waiting queue until either a thread that holds the lock invokes PulseAll, or it is the next in the queue and a thread that holds the lock invokes Pulse. However, if timeout milliseconds elapse before another thread invokes this object's Pulse or PulseAll method, the original thread is moved to the ready queue in order to regain the lock. Eventually the original thread will enter the ready queue and regain the lock. If the condition in the object's state has not been met, the thread may call Wait again to reenter the waiting queue until it has been met.
If timeout equals 0, the thread that calls Wait releases the lock and then immediately enters the ready queue in order to regain the lock.
Note that a synchronized object holds several references, including a reference to the thread that currently holds the lock, a reference to the ready queue, which contains the threads that are ready to obtain the lock, and a reference to the waiting queue, which contains the threads that are waiting for notification of a change in the object's state. The Pulse, PulseAll, and Wait methods must be invoked from within a synchronized block of code. The examples for Wait(Object, Int32) resolve an issue that arises when Pulse is invoked before Wait.
Monitor Class | Monitor Members | System.Threading Namespace | Monitor.Wait Overload List | Thread