Notifies a thread in the waiting queue of a change in the locked object's state.
Assembly: mscorlib (in mscorlib.dll)
Only the current owner of the lock can signal a waiting object using Pulse.
The thread that currently owns the lock on the specified object invokes this method to signal the next thread in line for the lock. Upon receiving the pulse, the waiting thread is moved to the ready queue. When the thread that invoked Pulse releases the lock, the next thread in the ready queue (which is not necessarily the thread that was pulsed) acquires the lock.
The Monitor class does not maintain state indicating that the method has been called. Thus, if you call when no threads are waiting, the next thread that calls Wait blocks as if had never been called. If two threads are using and Wait to interact, this could result in a deadlock. Contrast this with the behavior of the AutoResetEvent class: If you signal an AutoResetEvent by calling its Set method, and there are no threads waiting, the AutoResetEvent remains in a signaled state until a thread calls WaitOne, WaitAny, or WaitAll. The AutoResetEvent releases that thread and returns to the unsignaled state.
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.
To signal multiple threads, use the PulseAll method.
The following code example demonstrates how to use the Pulse 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 '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 End Class End Namespace