Notifies a waiting thread that an event has occurred. This class cannot be inherited.
Assembly: mscorlib (in mscorlib.dll)
Initializes a new instance of theclass with a Boolean value indicating whether to set the initial state to signaled.
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)
Determines whether the specified object is equal to the current object.(Inherited from Object.)
Serves as the default hash function. (Inherited from Object.)
Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)
Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)
Sets the state of the event to nonsignaled, causing threads to block.(Inherited from EventWaitHandle.)
Sets the state of the event to signaled, allowing one or more waiting threads to proceed.(Inherited from EventWaitHandle.)
Sets the access control security for a named system event.(Inherited from EventWaitHandle.)
Returns a string that represents the current object.(Inherited from Object.)
AutoResetEvent allows threads to communicate with each other by signaling. Typically, you use this class when threads need exclusive access to a resource.
This type implements the IDisposable interface. When you have finished using the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Dispose 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.
A thread waits for a signal by calling WaitOne on the AutoResetEvent. If the AutoResetEvent is in the non-signaled state, the thread blocks, waiting for the thread that currently controls the resource to signal that the resource is available by calling Set.
Calling Set signals AutoResetEvent to release a waiting thread. AutoResetEvent remains signaled until a single waiting thread is released, and then automatically returns to the non-signaled state. If no threads are waiting, the state remains signaled indefinitely.
If a thread calls WaitOne while the is in the signaled state, the thread does not block. The releases the thread immediately and returns to the non-signaled state.
There is no guarantee that every call to the Set method will release a thread. If two calls are too close together, so that the second call occurs before a thread has been released, only one thread is released. It is as if the second call did not happen. Also, if Set is called when there are no threads waiting and the is already signaled, the call has no effect.
You can control the initial state of an AutoResetEvent by passing a Boolean value to the constructor: true if the initial state is signaled and false otherwise.
For more information about thread synchronization mechanisms, see AutoResetEvent in the conceptual documentation.
Unlike the EventWaitHandle class provides access to named system synchronization events.class, the
The following example shows how to use Set method (on the base class) each time the user presses the Enter key. The example starts three threads, which wait on an that was created in the signaled state. The first thread is released immediately, because the is already in the signaled state. This resets the to the non-signaled state, so that subsequent threads block. The blocked threads are not released until the user releases them one at a time by pressing the Enter key.to release one thread at a time, by calling the
After the threads are released from the first Set method must be called three times to release them all., they wait on another that was created in the non-signaled state. All three threads block, so the
Available since 4.5
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Available since 8.1
This class is thread safe.