SignalAndWait Method (WaitHandle, WaitHandle)

WaitHandle.SignalAndWait Method (WaitHandle, WaitHandle)

Updated: April 2011

Signals one WaitHandle and waits on another.

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

public static bool SignalAndWait(
	WaitHandle toSignal,
	WaitHandle toWaitOn


Type: System.Threading.WaitHandle
The WaitHandle to signal.
Type: System.Threading.WaitHandle
The WaitHandle to wait on.

Return Value

Type: System.Boolean
true if both the signal and the wait complete successfully; if the wait does not complete, the method does not return.


toSignal is null.


toWaitOn is null.


The method was called on a thread that has STAThreadAttribute.


This method is not supported on Windows 98 or Windows Millennium Edition.


toSignal is a semaphore, and it already has a full count.


The wait completed because a thread exited without releasing a mutex. This exception is not thrown on Windows 98 or Windows Millennium Edition.

This operation is not guaranteed to be atomic. After the current thread signals toSignal but before it waits on toWaitOn, a thread that is running on another processor might signal toWaitOn or wait on it.

The following code example uses the SignalAndWait(WaitHandle, WaitHandle) method overload to allow the main thread to signal a blocked thread and then wait until the thread finishes a task.

The example starts five threads, allows them to block on an EventWaitHandle created with the EventResetMode.AutoReset flag, and then releases one thread each time the user presses the ENTER key. The example then queues another five threads and releases them all using an EventWaitHandle created with the EventResetMode.ManualReset flag.

using System;
using System.Threading;

public class Example
    // The EventWaitHandle used to demonstrate the difference
    // between AutoReset and ManualReset synchronization events.
    private static EventWaitHandle ewh;

    // A counter to make sure all threads are started and
    // blocked before any are released. A Long is used to show
    // the use of the 64-bit Interlocked methods.
    private static long threadCount = 0;

    // An AutoReset event that allows the main thread to block
    // until an exiting thread has decremented the count.
    private static EventWaitHandle clearCount = 
        new EventWaitHandle(false, EventResetMode.AutoReset);

    public static void Main()
        // Create an AutoReset EventWaitHandle.
        ewh = new EventWaitHandle(false, EventResetMode.AutoReset);

        // Create and start five numbered threads. Use the
        // ParameterizedThreadStart delegate, so the thread
        // number can be passed as an argument to the Start 
        // method.
        for (int i = 0; i <= 4; i++)
            Thread t = new Thread(
                new ParameterizedThreadStart(ThreadProc)

        // Wait until all the threads have started and blocked.
        // When multiple threads use a 64-bit value on a 32-bit
        // system, you must access the value through the
        // Interlocked class to guarantee thread safety.
        while (Interlocked.Read(ref threadCount) < 5)

        // Release one thread each time the user presses ENTER,
        // until all threads have been released.
        while (Interlocked.Read(ref threadCount) > 0)
            Console.WriteLine("Press ENTER to release a waiting thread.");

            // SignalAndWait signals the EventWaitHandle, which
            // releases exactly one thread before resetting, 
            // because it was created with AutoReset mode. 
            // SignalAndWait then blocks on clearCount, to 
            // allow the signaled thread to decrement the count
            // before looping again.
            WaitHandle.SignalAndWait(ewh, clearCount);

        // Create a ManualReset EventWaitHandle.
        ewh = new EventWaitHandle(false, EventResetMode.ManualReset);

        // Create and start five more numbered threads.
        for(int i=0; i<=4; i++)
            Thread t = new Thread(
                new ParameterizedThreadStart(ThreadProc)

        // Wait until all the threads have started and blocked.
        while (Interlocked.Read(ref threadCount) < 5)

        // Because the EventWaitHandle was created with
        // ManualReset mode, signaling it releases all the
        // waiting threads.
        Console.WriteLine("Press ENTER to release the waiting threads.");


    public static void ThreadProc(object data)
        int index = (int) data;

        Console.WriteLine("Thread {0} blocks.", data);
        // Increment the count of blocked threads.
        Interlocked.Increment(ref threadCount);

        // Wait on the EventWaitHandle.

        Console.WriteLine("Thread {0} exits.", data);
        // Decrement the count of blocked threads.
        Interlocked.Decrement(ref threadCount);

        // After signaling ewh, the main thread blocks on
        // clearCount until the signaled thread has 
        // decremented the count. Signal it now.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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




April 2011

Correction: The operation is not atomic.

Customer feedback.

Community Additions

© 2016 Microsoft