Releases the lock on an object and blocks the current thread until it reacquires the lock. If the specified time-out interval elapses, the thread enters the ready queue. This method also specifies whether the synchronization domain for the context (if in a synchronized context) is exited before the wait and reacquired afterward.
Namespace:
System.Threading
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Shared Function Wait ( _
obj As Object, _
millisecondsTimeout As Integer, _
exitContext As Boolean _
) As Boolean
Dim obj As Object
Dim millisecondsTimeout As Integer
Dim exitContext As Boolean
Dim returnValue As Boolean
returnValue = Monitor.Wait(obj, millisecondsTimeout, _
exitContext)
public static bool Wait(
Object obj,
int millisecondsTimeout,
bool exitContext
)
public:
static bool Wait(
Object^ obj,
int millisecondsTimeout,
bool exitContext
)
public static function Wait(
obj : Object,
millisecondsTimeout : int,
exitContext : boolean
) : boolean
Parameters
- obj
- Type: System..::.Object
The object on which to wait.
- millisecondsTimeout
- Type: System..::.Int32
The number of milliseconds to wait before the thread enters the ready queue.
- exitContext
- Type: System..::.Boolean
true to exit and reacquire the synchronization domain for the context (if in a synchronized context) before the wait; otherwise, false.
Return Value
Type:
System..::.Boolean
true if the lock was reacquired before the specified time elapsed; false if the lock was reacquired after the specified time elapsed. The method does not return until the lock is reacquired.
This method does not return until it reacquires an exclusive lock on the obj parameter.
The thread that currently owns the lock on the specified object invokes this method in order to release the object so that another thread can access it. The caller is blocked while waiting to reacquire the lock. This method is called when the caller needs to wait for a state change that will occur as a result of another thread's operations.
The time-out ensures that the current thread does not block indefinitely if another thread releases the lock without first calling the Pulse or PulseAll method. It also moves the thread to the ready queue, bypassing other threads ahead of it in the wait queue, so that it can reacquire the lock sooner. The thread can test the return value of the Wait method to determine whether it reacquired the lock prior to the time-out. The thread can evaluate the conditions that caused it to enter the wait, and if necessary call the Wait method again.
When a thread calls Wait, it releases the lock and enters the waiting queue. At this point, 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 millisecondsTimeout elapses 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.
Note: |
|---|
If Infinite is specified for the millisecondsTimeout parameter, this method blocks indefinitely unless the holder of the lock calls Pulse or PulseAll. If millisecondsTimeout equals 0, the thread that calls Wait releases the lock and then immediately enters the ready queue in order to regain the lock. |
The caller executes Wait once, regardless of the number of times Enter has been invoked for the specified object. Conceptually, the Wait method stores the number of times the caller invoked Enter on the object and invokes Exit as many times as necessary to fully release the locked object. The caller then blocks while waiting to reacquire the object. When the caller reacquires the lock, the system calls Enter as many times as necessary to restore the saved Enter count for the caller. Calling Wait releases the lock for the specified object only; if the caller is the owner of locks on other objects, these locks are not released.
Note: |
|---|
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 remarks for the Pulse method explain what happens if Pulse is called when no threads are waiting.
Notes on Exiting the Context
The exitContext parameter has no effect unless the Wait method is called from inside a nondefault managed context. This can happen if your thread is inside a call to an instance of a class derived from ContextBoundObject. Even if you are currently executing a method on a class that is not derived from ContextBoundObject, like String, you can be in a nondefault context if a ContextBoundObject is on your stack in the current application domain.
When your code is executing in a nondefault context, specifying true for exitContext causes the thread to exit the nondefault managed context (that is, to transition to the default context) before executing the Wait method. It returns to the original nondefault context after the call to the Wait method completes.
This can be useful when the context-bound class has the SynchronizationAttribute attribute applied. In that case, all calls to members of the class are automatically synchronized, and the synchronization domain is the entire body of code for the class. If code in the call stack of a member calls the Wait method and specifies true for exitContext, the thread exits the synchronization domain, allowing a thread that is blocked on a call to any member of the object to proceed. When the Wait method returns, the thread that made the call must wait to reenter the synchronization domain.
The following code example demonstrates how to use the Wait method.
Imports System
Imports System.Threading
Imports System.Collections
Namespace MonitorCS1
Class MonitorSample
Private MAX_LOOP_TIME As Integer = 1000
Private m_smplQueue As Queue
Public Sub New()
m_smplQueue = New Queue()
End Sub 'New
Public Sub FirstThread()
Dim counter As Integer = 0
SyncLock m_smplQueue
While counter < MAX_LOOP_TIME
'Wait, if the queue is busy.
Monitor.Wait(m_smplQueue)
'Push one element.
m_smplQueue.Enqueue(counter)
'Release the waiting thread.
Monitor.Pulse(m_smplQueue)
counter += 1
End While
End SyncLock
End Sub 'FirstThread
Public Sub SecondThread()
SyncLock m_smplQueue
'Release the waiting thread.
Monitor.Pulse(m_smplQueue)
'Wait in the loop while the queue is busy.
'Exit on the time-out when the first thread stops.
While Monitor.Wait(m_smplQueue, 1000)
'Pop the first element.
Dim counter As Integer = CInt(m_smplQueue.Dequeue())
'Print the first element.
Console.WriteLine(counter.ToString())
'Release the waiting thread.
Monitor.Pulse(m_smplQueue)
End While
End SyncLock
End Sub 'SecondThread
'Return the number of queue elements.
Public Function GetQueueCount() As Integer
Return m_smplQueue.Count
End Function 'GetQueueCount
Public Shared Sub Main(args() As String)
'Create the MonitorSample object.
Dim test As New MonitorSample()
'Create the first thread.
Dim tFirst As New Thread(AddressOf test.FirstThread)
'Create the second thread.
Dim tSecond As New Thread(AddressOf test.SecondThread)
'Start threads.
tFirst.Start()
tSecond.Start()
'wait to the end of the two threads
tFirst.Join()
tSecond.Join()
'Print the number of queue elements.
Console.WriteLine(("Queue Count = " + test.GetQueueCount().ToString()))
End Sub 'Main
End Class 'MonitorSample
End Namespace 'MonitorCS1
using System;
using System.Threading;
using System.Collections;
namespace MonitorCS1
{
class MonitorSample
{
const int MAX_LOOP_TIME = 1000;
Queue m_smplQueue;
public MonitorSample()
{
m_smplQueue = new Queue();
}
public void FirstThread()
{
int counter = 0;
lock(m_smplQueue)
{
while(counter < MAX_LOOP_TIME)
{
//Wait, if the queue is busy.
Monitor.Wait(m_smplQueue);
//Push one element.
m_smplQueue.Enqueue(counter);
//Release the waiting thread.
Monitor.Pulse(m_smplQueue);
counter++;
}
}
}
public void SecondThread()
{
lock(m_smplQueue)
{
//Release the waiting thread.
Monitor.Pulse(m_smplQueue);
//Wait in the loop, while the queue is busy.
//Exit on the time-out when the first thread stops.
while(Monitor.Wait(m_smplQueue,1000))
{
//Pop the first element.
int counter = (int)m_smplQueue.Dequeue();
//Print the first element.
Console.WriteLine(counter.ToString());
//Release the waiting thread.
Monitor.Pulse(m_smplQueue);
}
}
}
//Return the number of queue elements.
public int GetQueueCount()
{
return m_smplQueue.Count;
}
static void Main(string[] args)
{
//Create the MonitorSample object.
MonitorSample test = new MonitorSample();
//Create the first thread.
Thread tFirst = new Thread(new ThreadStart(test.FirstThread));
//Create the second thread.
Thread tSecond = new Thread(new ThreadStart(test.SecondThread));
//Start threads.
tFirst.Start();
tSecond.Start();
//wait to the end of the two threads
tFirst.Join();
tSecond.Join();
//Print the number of queue elements.
Console.WriteLine("Queue Count = " + test.GetQueueCount().ToString());
}
}
}
using namespace System;
using namespace System::Threading;
using namespace System::Collections;
ref class MonitorSample
{
public:
MonitorSample();
void FirstThread();
void SecondThread();
int GetQueueCount();
private:
literal int MAX_LOOP_TIME = 1000;
Queue^ m_smplQueue;
};
MonitorSample::MonitorSample()
{
m_smplQueue = gcnew Queue;
}
void MonitorSample::FirstThread()
{
int counter = 0;
Monitor::Enter( m_smplQueue );
while ( counter < MAX_LOOP_TIME )
{
//Wait, if the queue is busy.
Monitor::Wait( m_smplQueue );
//Push one element.
m_smplQueue->Enqueue( counter );
//Release the waiting thread.
Monitor::Pulse( m_smplQueue );
counter++;
}
Monitor::Exit( m_smplQueue );
}
void MonitorSample::SecondThread()
{
Monitor::Enter( m_smplQueue );
//Release the waiting thread.
Monitor::Pulse( m_smplQueue );
//Wait in the loop, while the queue is busy.
//Exit on the time-out when the first thread stopped.
while ( Monitor::Wait( m_smplQueue, 1000 ) )
{
//Pop the first element.
int counter = *dynamic_cast<int^>(m_smplQueue->Dequeue());
//Print the first element.
Console::WriteLine( counter.ToString() );
//Release the waiting thread.
Monitor::Pulse( m_smplQueue );
}
Monitor::Exit( m_smplQueue );
}
//Return the number of queue elements.
int MonitorSample::GetQueueCount()
{
return m_smplQueue->Count;
}
int main()
{
//Create the MonitorSample object.
MonitorSample^ test = gcnew MonitorSample;
//Create the first thread.
Thread^ tFirst = gcnew Thread( gcnew ThreadStart( test, &MonitorSample::FirstThread ) );
//Create the second thread.
Thread^ tSecond = gcnew Thread( gcnew ThreadStart( test, &MonitorSample::SecondThread ) );
//Start threads.
tFirst->Start();
tSecond->Start();
//wait to the end of the two threads
tFirst->Join();
tSecond->Join();
//Print the number of queue elements.
Console::WriteLine( String::Concat( "Queue Count = ", test->GetQueueCount().ToString() ) );
}
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Reference
Other Resources