WaitHandle Class

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

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

No code example is currently available or this language may not be supported.

The WaitHandle type exposes the following members.

  NameDescription
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360WaitHandleInitializes a new instance of the WaitHandle class.
Top

  NameDescription
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360SafeWaitHandleGets or sets the native operating-system handle.
Top

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360CloseWhen overridden in a derived class, releases all resources held by the current WaitHandle.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Dispose()Releases all resources used by the current instance of the WaitHandle class.
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Dispose(Boolean)When overridden in a derived class, releases the unmanaged resources used by the WaitHandle, and optionally releases the managed resources.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Equals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360FinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360ToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360WaitAll(WaitHandle[])Waits for all the elements in the specified array to receive a signal.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360WaitAll(WaitHandle[], Int32)Waits for all the elements in the specified array to receive a signal, using an Int32 value to specify the time interval.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360WaitAll(WaitHandle[], TimeSpan)Waits for all the elements in the specified array to receive a signal, using a TimeSpan value to specify the time interval.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360WaitAny(WaitHandle[])Waits for any of the elements in the specified array to receive a signal.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360WaitAny(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.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360WaitAny(WaitHandle[], TimeSpan)Waits for any of the elements in the specified array to receive a signal, using a TimeSpan to specify the time interval.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360WaitOne()Blocks the current thread until the current WaitHandle receives a signal.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360WaitOne(Int32)Blocks the current thread until the current WaitHandle receives a signal, using 32-bit signed integer to specify the time interval.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360WaitOne(TimeSpan)Blocks the current thread until the current instance receives a signal, using a TimeSpan to specify the time interval.
Top

  NameDescription
Protected fieldStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360InvalidHandleRepresents an invalid native operating-system handle. This field is read-only.
Public fieldStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360WaitTimeoutIndicates that a WaitAny operation timed out before any of the wait handles were signaled. This field is constant.
Top

This class is typically used as a base class for synchronization objects. Classes that are derived from WaitHandle define a signaling mechanism to indicate taking or releasing access to a shared resource, but they use the inherited WaitHandle methods to block while waiting for access to shared resources. In Silverlight, the AutoResetEvent and ManualResetEvent classes, along with their base class, EventWaitHandle, derive from WaitHandle.

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

The following example shows how to divide work among three thread pool threads, and how to use the static WaitAny and WaitAll methods to wait until the subtasks are finished.

The example creates a BackgroundWorker that reports progress to the user interface. By using a BackgroundWorker, the example insulates the user interface thread from the effects of the WaitAny and WaitAll methods, and thus allows the user interface to remain responsive.

The BackgroundWorker runs a DoWork method that creates three tasks by using the ThreadPool.QueueUserWorkItem method, and assigns each task a random amount of work. The example defines a Subtask class to hold the data and thread procedure for each task. Each task has a ManualResetEvent, which it signals when its work is complete.

After starting the tasks, the DoWork method uses the WaitAny(WaitHandle[], Int32) method overload to wait for the shortest subtask to finish, with a 250-millisecond time-out to report progress to the user interface. The BackgroundWorker then uses the WaitAll(WaitHandle[], Int32) method overload to wait until the rest of the tasks are complete, once again with a time-out to show progress. The DoWork method then produces a report using the results from all three tasks.

NoteNote:

The shortest task is not necessarily the first to complete. The thread pool threads may not all start immediately and may not be treated equally by the scheduler.

After you start the example, it changes the mouse button event to show user clicks, demonstrating that the user interface remains responsive during the execution of the background tasks.

No code example is currently available or this language may not be supported.

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

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

This type is thread safe.

Community Additions

ADD
Show: