Udostępnij za pośrednictwem


Monitor.Pulse Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Notifies a thread in the waiting queue of a change in the locked object's state.

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Sub Pulse ( _
    obj As Object _
)
[SecuritySafeCriticalAttribute]
public static void Pulse(
    Object obj
)

Parameters

Exceptions

Exception Condition
ArgumentNullException

The obj parameter is nulla null reference (Nothing in Visual Basic).

SynchronizationLockException

The calling thread does not own the lock for the specified object.

Remarks

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.

Important noteImportant Note:

The Monitor class does not maintain state indicating that the Pulse method has been called. Thus, if you call Pulse when no threads are waiting, the next thread that calls Wait blocks as if Pulse had never been called. If two threads are using Pulse 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.

The Pulse, PulseAll, and Wait methods must be invoked from within a synchronized block of code.

To signal multiple threads, use the PulseAll method.

Examples

The following example demonstrates how to use the Pulse method.

Imports System.Threading
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Windows.Input
Imports System.Windows.Controls

Class Example

    Const MAX_ITERATIONS As Integer = 100

    ' Example data: A BackgroundWorker that reports progress to the user 
    ' interface thread, the queue that is protected by the lock, and a
    ' random number generator.
    Private m_worker As New BackgroundWorker()
    Private m_smplQueue As New Queue(Of Integer)
    Private m_random As New Random()

    ' This thread does work and produces a result (in this case just a
    ' number).
    Private Sub Producer()
        Dim counter As Integer = 0
        SyncLock m_smplQueue
            While counter < MAX_ITERATIONS

                ' Wait, if the queue is busy.
                Monitor.Wait(m_smplQueue)

                ' Simulate a small amount of work, then queue one element
                ' and release the waiting thread.
                Thread.Sleep(10 + m_random.Next(10))
                m_smplQueue.Enqueue(counter)
                Monitor.Pulse(m_smplQueue)

                counter += 1
            End While
        End SyncLock
    End Sub 

    ' This thread consumes the result produced by the first thread. It 
    ' could do additional processing, but in this case it just reports
    ' the number.
    Private Sub Consumer()
        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.
                m_worker.ReportProgress(0, String.Format("{0} ", counter))

                ' Release the waiting thread.
                Monitor.Pulse(m_smplQueue)
            End While
       End SyncLock
    End Sub 


    ' -------------------
    ' This section contains supporting code that runs the example on a
    ' background thread, so it doesn't block the UI thread, and enables
    ' the example to display output using Silverlight UI elements. There
    ' is no example code for Monitor in this section.

    ' This UI element receives the output from the example. It's the same
    ' for all instances of Example.
    Private Shared outputBlock As TextBlock

    ' A list of all Example objects that are currently running. 
    Private Shared examples As New List(Of Example)

    ' The Demo method saves the TextBlock used for output, and sets up a
    ' mouse event that you click to run instances of the demonstration.
    '
    Public Shared Sub Demo(ByVal outputBlock As TextBlock)

        Example.outputBlock = outputBlock
        outputBlock.Text &= "Click here to begin running the example." & vbCrLf

        ' Set up an event handler to run the example when the TextBlock 
        ' is clicked.
        AddHandler outputBlock.MouseLeftButtonUp, AddressOf MouseUp
    End Sub

    ' This mouse event gives visual feedback and starts a new instance of
    ' Example. The instance must be started on the UI thread, so that the
    ' BackgroundWorker raises its events on the UI thread.
    '
    Private Shared Sub MouseUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs) 
        outputBlock.Text &= "<Click> "
        examples.Add(New Example())
    End Sub 


    ' Each new Example sets up the events for its background task and
    ' launches the task. It also creates a unique ID number.
    '
    Private m_id As Integer
    Private Shared lastId As Integer = 0
    Public Sub New()
        m_id = Interlocked.Increment(lastId)

        AddHandler m_worker.DoWork, AddressOf Me.RunExample
        m_worker.WorkerReportsProgress = True
        AddHandler m_worker.ProgressChanged, AddressOf Me.Progress
        AddHandler m_worker.RunWorkerCompleted, AddressOf Me.Completed
        m_worker.RunWorkerAsync()
    End Sub

    ' Launch the producer and consumer threads.
    Private Sub RunExample(ByVal sender As Object, ByVal e As DoWorkEventArgs)

        ' Create the threads and start them.
        Dim tProducer As New Thread(AddressOf Me.Producer)
        Dim tConsumer As New Thread(AddressOf Me.Consumer)
        tProducer.Start()
        Thread.Sleep(1)
        tConsumer.Start()

        ' Wait until the two threads end.
        tProducer.Join()
        tConsumer.Join()

        ' When the task completes, return the Example object.
        e.Result = Me
    End Sub

    ' The display output that is queued using the ReportProgress method
    ' is delivered to the UI thread by this event handler.
    '
    Private Sub Progress(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
        ' This code is executed on the main UI thread, so it's safe to 
        ' access the UI elements.
        outputBlock.Text &= e.UserState.ToString()
    End Sub

    ' This event handler signals task completion to the UI thread. It 
    ' removes the Example object from the list and displays its count.
    '
    Private Sub Completed(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
        examples.Remove(Me)
        outputBlock.Text &= String.Format("<Example({0}) Queue Count = {1}> ", _
                                          m_id, m_smplQueue.Count)
    End Sub
End Class 

' This code produces output similar to the following:
'
'Click here to begin running the example.
'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 
'30 <Click> 31 32 33 0 34 1 35 2 36 3 37 38 4 5 39 6 40 7 41 8 42 43 9 10 44 45 11
'46 12 13 47 48 14 49 15 50 16 51 17 52 18 53 19 54 20 55 21 56 22 57 23 24 58 25 
'59 26 60 27 28 61 29 62 30 31 63 64 32 33 65 34 66 35 67 36 68 37 69 38 39 70 71
'40 41 72 42 73 74 43 75 44 76 77 45 46 78 47 79 48 80 49 81 50 82 51 83 52 84 53
'85 54 86 55 87 56 88 57 89 58 90 59 91 60 92 61 93 62 94 63 95 64 65 96 97 66 98 
'67 99 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
'93 94 95 96 97 98 99 <Example(1) Queue Count = 0> <Example(2) Queue Count = 0> 
using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows.Controls;

class Example
{
    const int MAX_ITERATIONS = 100;

    // Example data: A BackgroundWorker that reports progress to the user 
    // interface thread, the queue that is protected by the lock, and a
    // random number generator.
    private BackgroundWorker m_worker = new BackgroundWorker();
    private Queue<int> m_smplQueue = new Queue<int>();
    private Random m_random = new Random();

    // This thread does work and produces a result (in this case just a
    // number).
    private void Producer()
    {
        int counter = 0;
        lock (m_smplQueue)
        {
            while (counter < MAX_ITERATIONS)
            {
                // Wait, if the queue is busy.
                Monitor.Wait(m_smplQueue);

                // Simulate a small amount of work, then queue one element
                // and release the waiting thread.
                Thread.Sleep(10 + m_random.Next(10));
                m_smplQueue.Enqueue(counter);
                Monitor.Pulse(m_smplQueue);

                counter += 1;
            }
        }
    }

    // This thread consumes the result produced by the first thread. It 
    // could do additional processing, but in this case it just reports
    // the number.
    private void Consumer()
    {
        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.
                m_worker.ReportProgress(0, String.Format("{0} ", counter));

                // Release the waiting thread.
                Monitor.Pulse(m_smplQueue);
            }
        }
    }


    // -------------------
    // This section contains supporting code that runs the example on a
    // background thread, so it doesn't block the UI thread, and enables
    // the example to display output using Silverlight UI elements. There
    // is no example code for Monitor in this section.

    // This UI element receives the output from the example. It's the same
    // for all instances of Example.
    private static TextBlock outputBlock;

    // A list of all Example objects that are currently running. 
    private static List<Example> examples = new List<Example>();

    // The Demo method saves the TextBlock used for output, and sets up a
    // mouse event that you click to run instances of the demonstration.
    //
    public static void Demo(TextBlock outputBlock)
    {
        Example.outputBlock = outputBlock;
        outputBlock.Text += "Click here to begin running the example.\r\n";

        // Set up an event handler to run the example when the TextBlock 
        // is clicked.
        outputBlock.MouseLeftButtonUp += MouseUp;
    }

    // This mouse event gives visual feedback and starts a new instance of
    // Example. The instance must be started on the UI thread, so that the
    // BackgroundWorker raises its events on the UI thread.
    //
    private static void MouseUp(object sender, MouseButtonEventArgs e)
    {
        outputBlock.Text += "<Click> ";
        examples.Add(new Example());
    }


    // Each new Example sets up the events for its background task and
    // launches the task. It also creates a unique ID number.
    //
    private int m_id;
    private static int lastId = 0;
    public Example()
    {
        m_id = Interlocked.Increment(ref lastId);

        m_worker.DoWork += this.RunExample;
        m_worker.WorkerReportsProgress = true;
        m_worker.ProgressChanged += this.Progress;
        m_worker.RunWorkerCompleted += this.Completed;
        m_worker.RunWorkerAsync();
    }

    // Launch the producer and consumer threads.
    private void RunExample(object sender, DoWorkEventArgs e)
    {

        // Create the threads and start them.
        Thread tProducer = new Thread(this.Producer);
        Thread tConsumer = new Thread(this.Consumer);
        tProducer.Start();
        Thread.Sleep(1);
        tConsumer.Start();

        // Wait until the two threads end.
        tProducer.Join();
        tConsumer.Join();

        // When the task completes, return the Example object.
        e.Result = this;
    }

    // The display output that is queued using the ReportProgress method
    // is delivered to the UI thread by this event handler.
    //
    private void Progress(object sender, ProgressChangedEventArgs e)
    {
        // This code is executed on the main UI thread, so it's safe to 
        // access the UI elements.
        outputBlock.Text += e.UserState.ToString();
    }

    // This event handler signals task completion to the UI thread. It 
    // removes the Example object from the list and displays its count.
    //
    private void Completed(object sender, RunWorkerCompletedEventArgs e)
    {
        examples.Remove(this);
        outputBlock.Text += String.Format("<Example({0}) Queue Count = {1}> ",
                                          m_id, m_smplQueue.Count);
    }
}

/* This code produces output similar to the following:

Click here to begin running the example.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 
30 <Click> 31 32 33 0 34 1 35 2 36 3 37 38 4 5 39 6 40 7 41 8 42 43 9 10 44 45 11
46 12 13 47 48 14 49 15 50 16 51 17 52 18 53 19 54 20 55 21 56 22 57 23 24 58 25
59 26 60 27 28 61 29 62 30 31 63 64 32 33 65 34 66 35 67 36 68 37 69 38 39 70 71 
40 41 72 42 73 74 43 75 44 76 77 45 46 78 47 79 48 80 49 81 50 82 51 83 52 84 53
85 54 86 55 87 56 88 57 89 58 90 59 91 60 92 61 93 62 94 63 95 64 65 96 97 66 98
67 99 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
93 94 95 96 97 98 99 <Example(1) Queue Count = 0> <Example(2) Queue Count = 0> 
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.