Waits for notification passed by the execution engine from an object, which called the Pulse or PulseAll method.
[Visual Basic] Overloads Public Shared Function Wait( _ ByVal obj As Object _ ) As Boolean [C#] public static bool Wait( object obj ); [C++] public: static bool Wait( Object* obj ); [JScript] public static function Wait( obj : Object ) : Boolean;
True if the wait succeeded; otherwise, false.
Exception Type | Condition |
---|---|
ArgumentNullException | If obj is a null reference (in Visual Basic Nothing). |
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. (After a call to this object's Pulse (or PulseAll) method, a single thread (or all the threads, respectively) will leave the waiting queue and enter the ready queue.) Eventually the original thread will 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. Note that if neither Pulse nor PulseAll are called; the threads in the waiting queue will not be moved to the ready queue and the process will hang.
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 in the description 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