In the .NET Framework version 2.0, AutoResetEvent derives from the new EventWaitHandle class. An AutoResetEvent is functionally equivalent to an EventWaitHandle created with EventResetMode..::.AutoReset.
Note: |
|---|
Unlike the
AutoResetEvent class, the EventWaitHandle class provides access to named system synchronization events.
|
AutoResetEvent allows threads to communicate with each other by signaling. Typically, this communication concerns a resource to which threads need exclusive access.
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 AutoResetEvent is in the signaled state, the thread does not block. The AutoResetEvent releases the thread immediately and returns to the non-signaled state.
Important Note: |
|---|
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 AutoResetEvent 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.
AutoResetEvent can also be used with the static WaitAll and WaitAny methods.
For more information about thread synchronization mechanisms, see AutoResetEvent in the conceptual documentation.