WaitHandle Class
TOC
Collapse the table of content
Expand the table of content

WaitHandle Class

 

Encapsulates operating system–specific objects that wait for exclusive access to shared resources.

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

System.Object
  System.MarshalByRefObject
    System.Threading.WaitHandle
      System.Threading.EventWaitHandle
      System.Threading.Mutex
      System.Threading.Semaphore

[ComVisibleAttribute(true)]
public abstract class WaitHandle : MarshalByRefObject, IDisposable

NameDescription
System_CAPS_protmethodWaitHandle()

Initializes a new instance of the WaitHandle class.

NameDescription
System_CAPS_pubpropertyHandle

Obsolete.Gets or sets the native operating system handle.

System_CAPS_pubpropertySafeWaitHandle

Gets or sets the native operating system handle.

NameDescription
System_CAPS_pubmethodClose()

Releases all resources held by the current WaitHandle.

System_CAPS_pubmethodCreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodDispose()

Releases all resources used by the current instance of the WaitHandle class.

System_CAPS_protmethodDispose(Boolean)

When overridden in a derived class, releases the unmanaged resources used by the WaitHandle, and optionally releases the managed resources.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodInitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodSystem_CAPS_staticSignalAndWait(WaitHandle, WaitHandle)

Signals one WaitHandle and waits on another.

System_CAPS_pubmethodSystem_CAPS_staticSignalAndWait(WaitHandle, WaitHandle, Int32, Boolean)

Signals one WaitHandle and waits on another, specifying a time-out interval as a 32-bit signed integer and specifying whether to exit the synchronization domain for the context before entering the wait.

System_CAPS_pubmethodSystem_CAPS_staticSignalAndWait(WaitHandle, WaitHandle, TimeSpan, Boolean)

Signals one WaitHandle and waits on another, specifying the time-out interval as a TimeSpan and specifying whether to exit the synchronization domain for the context before entering the wait.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_pubmethodSystem_CAPS_staticWaitAll(WaitHandle[])

Waits for all the elements in the specified array to receive a signal.

System_CAPS_pubmethodSystem_CAPS_staticWaitAll(WaitHandle[], Int32)

Waits for all the elements in the specified array to receive a signal, using an Int32 value to specify the time interval.

System_CAPS_pubmethodSystem_CAPS_staticWaitAll(WaitHandle[], Int32, Boolean)

Waits for all the elements in the specified array to receive a signal, using an Int32 value to specify the time interval and specifying whether to exit the synchronization domain before the wait.

System_CAPS_pubmethodSystem_CAPS_staticWaitAll(WaitHandle[], TimeSpan)

Waits for all the elements in the specified array to receive a signal, using a TimeSpan value to specify the time interval.

System_CAPS_pubmethodSystem_CAPS_staticWaitAll(WaitHandle[], TimeSpan, Boolean)

Waits for all the elements in the specified array to receive a signal, using a TimeSpan value to specify the time interval, and specifying whether to exit the synchronization domain before the wait.

System_CAPS_pubmethodSystem_CAPS_staticWaitAny(WaitHandle[])

Waits for any of the elements in the specified array to receive a signal.

System_CAPS_pubmethodSystem_CAPS_staticWaitAny(WaitHandle[], Int32)

Waits for any of the elements in the specified array to receive a signal, using a 32-bit signed integer to specify the time interval.

System_CAPS_pubmethodSystem_CAPS_staticWaitAny(WaitHandle[], Int32, Boolean)

Waits for any of the elements in the specified array to receive a signal, using a 32-bit signed integer to specify the time interval, and specifying whether to exit the synchronization domain before the wait.

System_CAPS_pubmethodSystem_CAPS_staticWaitAny(WaitHandle[], TimeSpan)

Waits for any of the elements in the specified array to receive a signal, using a TimeSpan to specify the time interval.

System_CAPS_pubmethodSystem_CAPS_staticWaitAny(WaitHandle[], TimeSpan, Boolean)

Waits for any of the elements in the specified array to receive a signal, using a TimeSpan to specify the time interval and specifying whether to exit the synchronization domain before the wait.

System_CAPS_pubmethodWaitOne()

Blocks the current thread until the current WaitHandle receives a signal.

System_CAPS_pubmethodWaitOne(Int32)

Blocks the current thread until the current WaitHandle receives a signal, using a 32-bit signed integer to specify the time interval in milliseconds.

System_CAPS_pubmethodWaitOne(Int32, Boolean)

Blocks the current thread until the current WaitHandle receives a signal, using a 32-bit signed integer to specify the time interval and specifying whether to exit the synchronization domain before the wait.

System_CAPS_pubmethodWaitOne(TimeSpan)

Blocks the current thread until the current instance receives a signal, using a TimeSpan to specify the time interval.

System_CAPS_pubmethodWaitOne(TimeSpan, Boolean)

Blocks the current thread until the current instance receives a signal, using a TimeSpan to specify the time interval and specifying whether to exit the synchronization domain before the wait.

NameDescription
System_CAPS_protfieldSystem_CAPS_staticInvalidHandle

Represents an invalid native operating system handle. This field is read-only.

System_CAPS_pubfieldSystem_CAPS_staticWaitTimeout

Indicates that a WaitAny operation timed out before any of the wait handles were signaled. This field is constant.

NameDescription
System_CAPS_pubmethodGetSafeWaitHandle()

Gets the safe handle for a native operating system wait handle. (Defined by WaitHandleExtensions.)

System_CAPS_pubmethodSetSafeWaitHandle(SafeWaitHandle)

Sets a safe handle for a native operating system wait handle. (Defined by WaitHandleExtensions.)

This class is typically used as a base class for synchronization objects. Classes derived from WaitHandle define a signaling mechanism to indicate taking or releasing access to a shared resource, but use the inherited WaitHandle methods to block while waiting for access to shared resources.

System_CAPS_importantImportant

This type implements the IDisposable interface. When you have finished using the type or a type derived from it, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Close method in a try/catch block. To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). For more information, see the “Using an Object that Implements IDisposable” section in the IDisposable interface topic.

Use the static methods of this class to block a thread until one or more synchronization objects receive a signal.

WaitHandle implements the Dispose pattern. See Dispose Pattern. When you derive from WaitHandle, use the SafeWaitHandle property to store your native handle operating system handle. You do not need to override the protected Dispose method unless you use additional unmanaged resources.

The following code example shows how two threads can do background tasks while the Main thread waits for the tasks to complete using the static WaitAny and WaitAll methods of the WaitHandle class.

using System;
using System.Threading;

public sealed class App 
{
    // Define an array with two AutoResetEvent WaitHandles.
    static WaitHandle[] waitHandles = new WaitHandle[] 
    {
        new AutoResetEvent(false),
        new AutoResetEvent(false)
    };

    // Define a random number generator for testing.
    static Random r = new Random();

    static void Main() 
    {
        // Queue up two tasks on two different threads; 
        // wait until all tasks are completed.
        DateTime dt = DateTime.Now;
        Console.WriteLine("Main thread is waiting for BOTH tasks to complete.");
        ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
        ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
        WaitHandle.WaitAll(waitHandles);
        // The time shown below should match the longest task.
        Console.WriteLine("Both tasks are completed (time waited={0})", 
            (DateTime.Now - dt).TotalMilliseconds);

        // Queue up two tasks on two different threads; 
        // wait until any tasks are completed.
        dt = DateTime.Now;
        Console.WriteLine();
        Console.WriteLine("The main thread is waiting for either task to complete.");
        ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
        ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
        int index = WaitHandle.WaitAny(waitHandles);
        // The time shown below should match the shortest task.
        Console.WriteLine("Task {0} finished first (time waited={1}).",
            index + 1, (DateTime.Now - dt).TotalMilliseconds);
    }

    static void DoTask(Object state) 
    {
        AutoResetEvent are = (AutoResetEvent) state;
        int time = 1000 * r.Next(2, 10);
        Console.WriteLine("Performing a task for {0} milliseconds.", time);
        Thread.Sleep(time);
        are.Set();
    }
}

// This code produces output similar to the following:
//
//  Main thread is waiting for BOTH tasks to complete.
//  Performing a task for 7000 milliseconds.
//  Performing a task for 4000 milliseconds.
//  Both tasks are completed (time waited=7064.8052)
// 
//  The main thread is waiting for either task to complete.
//  Performing a task for 2000 milliseconds.
//  Performing a task for 2000 milliseconds.
//  Task 1 finished first (time waited=2000.6528).

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1

This type is thread safe.

Return to top
Show:
© 2016 Microsoft