Export (0) Print
Expand All
2 out of 2 rated this helpful - Rate this topic

Monitor.Pulse Method

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

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

public static void Pulse (
	Object obj
)
public static void Pulse (
	Object obj
)
public static function Pulse (
	obj : Object
)

Parameters

obj

The object a thread is waiting for.

Exception typeCondition

ArgumentNullException

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

SynchronizationLockException

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

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.

NoteImportant:

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.

The following code example demonstrates how to use the Pulse method.

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());
		}
	}
}


package MonitorJSL1;

import System.*;
import System.Threading.*;
import System.Collections.*;

class MonitorSample
{
    private int MAX_LOOP_TIME = 1000;
    private Queue mSmplQueue;

    public MonitorSample()
    {
        mSmplQueue = new Queue();
    } //MonitorSample

    public void FirstThread()
    {
        int counter = 0;
        synchronized (mSmplQueue) {
            while (counter < MAX_LOOP_TIME) {
                //Wait, if the queue is busy.
                Monitor.Wait(mSmplQueue);

                //Push one element.
                mSmplQueue.Enqueue((Int32)counter);

                //Release the waiting thread.
                Monitor.Pulse(mSmplQueue);
                counter++;
            }
        }
    } //FirstThread

    public void SecondThread()
    {
        synchronized (mSmplQueue) {
            //Release the waiting thread.
            Monitor.Pulse(mSmplQueue);

            //Wait in the loop, while the queue is busy.
            //Exit on the time-out when the first thread stops. 
            while (Monitor.Wait(mSmplQueue, 1000)) {
                //Pop the first element.
                int counter = Convert.ToInt32(mSmplQueue.Dequeue());

                //Print the first element.
                Console.WriteLine(Convert.ToString(counter));

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

    //Return the number of queue elements.
    public int GetQueueCount()
    {
        return mSmplQueue.get_Count();
    } //GetQueueCount

    public static void main(String[] args)
    {
        //Create the MonitorSample object.
        MonitorSample test = new MonitorSample();

        //Create the first thread.
        System.Threading.Thread tFirst = 
            new System.Threading.Thread(new ThreadStart(test.FirstThread));

        //Create the second thread.
        System.Threading.Thread tSecond = 
            new System.Threading.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 = "
            + Convert.ToString(test.GetQueueCount()));
    } //main
} //MonitorSample

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.