AutoResetEvent Class
Updated: March 2011
Notifies a waiting thread that an event has occurred. This class cannot be inherited.
System::MarshalByRefObject
System.Threading::WaitHandle
System.Threading::EventWaitHandle
System.Threading::AutoResetEvent
Assembly: mscorlib (in mscorlib.dll)
The AutoResetEvent type exposes the following members.
| Name | Description | |
|---|---|---|
![]() ![]() ![]() | AutoResetEvent | Initializes a new instance of the AutoResetEvent class with a Boolean value indicating whether to set the initial state to signaled. |
| Name | Description | |
|---|---|---|
![]() ![]() | Handle | Obsolete. Gets or sets the native operating system handle. (Inherited from WaitHandle.) |
![]() | SafeWaitHandle | Gets or sets the native operating system handle. (Inherited from WaitHandle.) |
| Name | Description | |
|---|---|---|
![]() ![]() | Close | When overridden in a derived class, releases all resources held by the current WaitHandle. (Inherited from WaitHandle.) In XNA Framework, this member is overridden by Close(). |
![]() | CreateObjRef | Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.) |
![]() ![]() | Dispose() | Releases all resources used by the current instance of the WaitHandle class. (Inherited from WaitHandle.) |
![]() ![]() ![]() | Dispose(Boolean) | When overridden in a derived class, releases the unmanaged resources used by the WaitHandle, and optionally releases the managed resources. (Inherited from WaitHandle.) |
![]() ![]() ![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() ![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) In XNA Framework, this member is overridden by Finalize(). |
![]() | GetAccessControl | Gets an EventWaitHandleSecurity object that represents the access control security for the named system event represented by the current EventWaitHandle object. (Inherited from EventWaitHandle.) |
![]() ![]() ![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetLifetimeService | Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.) |
![]() ![]() ![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | InitializeLifetimeService | Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.) |
![]() ![]() ![]() | MemberwiseClone() | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | MemberwiseClone(Boolean) | Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.) |
![]() ![]() ![]() | Reset | Sets the state of the event to nonsignaled, causing threads to block. (Inherited from EventWaitHandle.) |
![]() ![]() ![]() | Set | Sets the state of the event to signaled, allowing one or more waiting threads to proceed. (Inherited from EventWaitHandle.) |
![]() | SetAccessControl | Sets the access control security for a named system event. (Inherited from EventWaitHandle.) |
![]() ![]() ![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
![]() ![]() | WaitOne() | Blocks the current thread until the current WaitHandle receives a signal. (Inherited from WaitHandle.) In XNA Framework, this member is overridden by WaitOne(). |
![]() | WaitOne(Int32) | Blocks the current thread until the current WaitHandle receives a signal, using a 32-bit signed integer to specify the time interval. (Inherited from WaitHandle.) |
![]() | WaitOne(TimeSpan) | Blocks the current thread until the current instance receives a signal, using a TimeSpan to specify the time interval. (Inherited from WaitHandle.) |
![]() ![]() | WaitOne(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. (Inherited from WaitHandle.) In XNA Framework, this member is overridden by WaitOne(Int32, Boolean). |
![]() | WaitOne(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. (Inherited from WaitHandle.) |
| Name | Description | |
|---|---|---|
![]() ![]() ![]() | IDisposable::Dispose | Infrastructure. Releases all resources used by the WaitHandle. (Inherited from WaitHandle.) |
AutoResetEvent allows threads to communicate with each other by signaling. Typically, you use this class when threads need exclusive access to a resource.
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 |
|---|
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.
Beginning with 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. |
Note |
|---|
The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: Synchronization | ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes. |
The following example shows how to use AutoResetEvent to release one thread at a time, by calling the Set method (on the base class) each time the user presses the Enter key. The example starts three threads, which wait on an AutoResetEvent that was created in the signaled state. The first thread is released immediately, because the AutoResetEvent is already in the signaled state. This resets the AutoResetEvent 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.
After the threads are released from the first AutoResetEvent, they wait on another AutoResetEvent that was created in the non-signaled state. All three threads block, so the Set method must be called three times to release them all.
using namespace System; using namespace System::Threading; ref class Example { private: static AutoResetEvent^ event_1 = gcnew AutoResetEvent(true); static AutoResetEvent^ event_2 = gcnew AutoResetEvent(false); static void ThreadProc() { String^ name = Thread::CurrentThread->Name; Console::WriteLine("{0} waits on AutoResetEvent #1.", name); event_1->WaitOne(); Console::WriteLine("{0} is released from AutoResetEvent #1.", name); Console::WriteLine("{0} waits on AutoResetEvent #2.", name); event_2->WaitOne(); Console::WriteLine("{0} is released from AutoResetEvent #2.", name); Console::WriteLine("{0} ends.", name); } public: static void Demo() { Console::WriteLine("Press Enter to create three threads and start them.\r\n" + "The threads wait on AutoResetEvent #1, which was created\r\n" + "in the signaled state, so the first thread is released.\r\n" + "This puts AutoResetEvent #1 into the unsignaled state."); Console::ReadLine(); for (int i = 1; i < 4; i++) { Thread^ t = gcnew Thread(gcnew ThreadStart(&ThreadProc)); t->Name = "Thread_" + i; t->Start(); } Thread::Sleep(250); for (int i = 0; i < 2; i++) { Console::WriteLine("Press Enter to release another thread."); Console::ReadLine(); event_1->Set(); Thread::Sleep(250); } Console::WriteLine("\r\nAll threads are now waiting on AutoResetEvent #2."); for (int i = 0; i < 3; i++) { Console::WriteLine("Press Enter to release a thread."); Console::ReadLine(); event_2->Set(); Thread::Sleep(250); } // Visual Studio: Uncomment the following line. //Console::Readline(); } }; void main() { Example::Demo(); } /* This example produces output similar to the following: Press Enter to create three threads and start them. The threads wait on AutoResetEvent #1, which was created in the signaled state, so the first thread is released. This puts AutoResetEvent #1 into the unsignaled state. Thread_1 waits on AutoResetEvent #1. Thread_1 is released from AutoResetEvent #1. Thread_1 waits on AutoResetEvent #2. Thread_3 waits on AutoResetEvent #1. Thread_2 waits on AutoResetEvent #1. Press Enter to release another thread. Thread_3 is released from AutoResetEvent #1. Thread_3 waits on AutoResetEvent #2. Press Enter to release another thread. Thread_2 is released from AutoResetEvent #1. Thread_2 waits on AutoResetEvent #2. All threads are now waiting on AutoResetEvent #2. Press Enter to release a thread. Thread_2 is released from AutoResetEvent #2. Thread_2 ends. Press Enter to release a thread. Thread_1 is released from AutoResetEvent #2. Thread_1 ends. Press Enter to release a thread. Thread_3 is released from AutoResetEvent #2. Thread_3 ends. */
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.







Important
Note