共用方式為


Monitor.Wait Method (Object, Int32)

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

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.

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

Syntax

'Declaration
Public Shared Function Wait ( _
    obj As Object, _
    millisecondsTimeout As Integer _
) As Boolean
public static bool Wait(
    Object obj,
    int millisecondsTimeout
)

Parameters

  • millisecondsTimeout
    Type: System.Int32
    The number of milliseconds to wait before the thread enters the ready queue.

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.

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.

ArgumentOutOfRangeException

The value of the millisecondsTimeout parameter is negative, and is not equal to Infinite.

Remarks

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 on the object and enters the object's waiting queue. The next thread in the object's ready queue (if there is one) acquires the lock and has exclusive use of the object. 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.

NoteNote:

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.

NoteNote:

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.

Version Notes

Silverlight for Windows Phone Silverlight for Windows Phone

When a user navigates away from a Windows Phone application, the application is typically put into a dormant state. When the user returns to a dormant application, the application automatically resumes. If the application is put into a dormant state while this API is being used, the API will not complete as expected. Applications should be designed to handle this possibility. For more information about the Windows Phone execution model, see Execution Model for Windows Phone.

Examples

The following example demonstrates how to use the Wait method with a time-out. The method times out when the Producer thread is no longer supplying new values.

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.