WaitHandle Class (System.Threading)
发布时间:2026-09-10 | 浏览:1
Access to this page requires authorization. You can try signing in or changing directories .
Access to this page requires authorization. You can try changing directories .
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Encapsulates operating system-specific objects that wait for exclusive access to shared 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.
The WaitHandle class encapsulates a native operating system synchronization handle and is used to represent all synchronization objects in the runtime that allow multiple wait operations. For a comparison of wait handles with other synchronization objects, see Overview of Synchronization Primitives .
The WaitHandle class itself is abstract. Classes 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. The classes derived from WaitHandle include:
The Mutex class. See Mutexes .
The Mutex class. See Mutexes .
The EventWaitHandle class and its derived classes, AutoResetEvent and ManualResetEvent .
The EventWaitHandle class and its derived classes, AutoResetEvent and ManualResetEvent .
The Semaphore class. See Semaphore and SemaphoreSlim .
The Semaphore class. See Semaphore and SemaphoreSlim .
Threads can block on an individual wait handle by calling the instance method WaitOne , which is inherited by classes derived from WaitHandle .
The derived classes of WaitHandle differ in their thread affinity. Event wait handles ( EventWaitHandle , AutoResetEvent , and ManualResetEvent ) and semaphores do not have thread affinity; any thread can signal an event wait handle or semaphore. Mutexes, on the other hand, do have thread affinity; the thread that owns a mutex must release it, and an exception is thrown if a thread calls the ReleaseMutex method on a mutex that it does not own.
Because the WaitHandle class derives from MarshalByRefObject , these classes can be used to synchronize the activities of threads across application domain boundaries.
In addition to its derived classes, the WaitHandle class has a number of static methods that block a thread until one or more synchronization objects receive a signal. These include:
SignalAndWait , which allows a thread to signal one wait handle and immediately wait on another.
SignalAndWait , which allows a thread to signal one wait handle and immediately wait on another.
WaitAll , which allows a thread to wait until all the wait handles in an array receive a signal.
WaitAll , which allows a thread to wait until all the wait handles in an array receive a signal.
WaitAny , which allows a thread to wait until any one of a specified set of wait handles has been signaled.
WaitAny , which allows a thread to wait until any one of a specified set of wait handles has been signaled.
The overloads of these methods provide timeout intervals for abandoning the wait, and the opportunity to exit a synchronization context before entering the wait, allowing other threads to use the synchronization context.
This type implements the IDisposable interface. When you've 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 / finally block. To dispose of it indirectly, use a language construct such as using in C#.
When you derive from WaitHandle , use the SafeWaitHandle property to store your native operating system handle. You do not need to override the protected Dispose method unless you use additional unmanaged resources.
Initializes a new instance of the WaitHandle class.
Represents an invalid native operating system handle. This field is read-only.
Indicates that a WaitAny(WaitHandle[], Int32, Boolean) operation timed out before any of the wait handles were signaled. This field is constant.
Gets or sets the native operating system handle.
Gets or sets the native operating system handle.
Releases all resources held by the current WaitHandle .
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
Releases all resources used by the current instance of the WaitHandle class.
When overridden in a derived class, releases the unmanaged resources used by the WaitHandle , and optionally releases the managed resources.
Determines whether the specified object is equal to the current object.
Releases the resources held by the current instance.
Serves as the default hash function.
Retrieves the current lifetime service object that controls the lifetime policy for this instance.
Gets the Type of the current instance.
Obtains a lifetime service object to control the lifetime policy for this instance.
Creates a shallow copy of the current Object .
Creates a shallow copy of the current MarshalByRefObject object.
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.
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.
Signals one WaitHandle and waits on another.
Returns a string that represents the current object.
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.
Waits for all the elements in the specified array to receive a signal, using an Int32 value to specify the time interval.
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.
Waits for all the elements in the specified array to receive a signal, using a TimeSpan value to specify the time interval.
Waits for all the elements in the specified array to receive a signal.
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.
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.
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.
Waits for any of the elements in the specified array to receive a signal, using a TimeSpan to specify the time interval.
Waits for any of the elements in the specified array to receive a signal.
Blocks the current thread until the current WaitHandle receives a signal.
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.
Blocks the current thread until the current WaitHandle receives a signal, using a 32-bit signed integer to specify the time interval in milliseconds.
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.
Blocks the current thread until the current instance receives a signal, using a TimeSpan to specify the time interval.
Explicit Interface Implementations
This API supports the product infrastructure and is not intended to be used directly from your code.
Releases all resources used by the WaitHandle .
Extension Methods
Gets the safe handle for a native operating system wait handle.
Sets a safe handle for a native operating system wait handle.
This type is thread safe.
Threading Objects and Features
EventWaitHandle, AutoResetEvent, and ManualResetEvent
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?