AutoResetEvent
Class
Definition
Notifies a waiting thread that an event has occurred. This class cannot be inherited.
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class AutoResetEvent : System.Threading.EventWaitHandle
- Inheritance
- Attributes
Inherited Members
System.MarshalByRefObject
System.Object
System.Threading.EventWaitHandle
System.Threading.WaitHandle
Examples
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.
*/
using System;
using System.Threading;
// Visual Studio: Replace the default class in a Console project with
// the following class.
class Example
{
private static AutoResetEvent event_1 = new AutoResetEvent(true);
private static AutoResetEvent event_2 = new AutoResetEvent(false);
static void Main()
{
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 = new Thread(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();
}
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);
}
}
/* 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.
*/
Imports System.Threading
' Visual Studio: Replace the default class in a Console project with
' the following class.
Class Example
Private Shared event_1 As New AutoResetEvent(True)
Private Shared event_2 As New AutoResetEvent(False)
<MTAThread()> _
Shared Sub Main()
Console.WriteLine("Press Enter to create three threads and start them." & vbCrLf & _
"The threads wait on AutoResetEvent #1, which was created" & vbCrLf & _
"in the signaled state, so the first thread is released." & vbCrLf & _
"This puts AutoResetEvent #1 into the unsignaled state.")
Console.ReadLine()
For i As Integer = 1 To 3
Dim t As New Thread(AddressOf ThreadProc)
t.Name = "Thread_" & i
t.Start()
Next
Thread.Sleep(250)
For i As Integer = 1 To 2
Console.WriteLine("Press Enter to release another thread.")
Console.ReadLine()
event_1.Set()
Thread.Sleep(250)
Next
Console.WriteLine(vbCrLf & "All threads are now waiting on AutoResetEvent #2.")
For i As Integer = 1 To 3
Console.WriteLine("Press Enter to release a thread.")
Console.ReadLine()
event_2.Set()
Thread.Sleep(250)
Next
' Visual Studio: Uncomment the following line.
'Console.Readline()
End Sub
Shared Sub ThreadProc()
Dim name As String = 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)
End Sub
End Class
' 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.
Remarks
AutoResetEvent allows threads to communicate with each other by signaling. Typically, you use this class when threads need exclusive access to a resource.
Important
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 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 staticWaitAll 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.
Note
Unlike the AutoResetEvent class, the EventWaitHandle class provides access to named system synchronization events.
Constructors
| AutoResetEvent(Boolean) |
Initializes a new instance of the AutoResetEvent class with a Boolean value indicating whether to set the initial state to signaled. |
Extension Methods
| GetAccessControl(EventWaitHandle) | |
| SetAccessControl(EventWaitHandle, EventWaitHandleSecurity) | |
| GetSafeWaitHandle(WaitHandle) | |
| SetSafeWaitHandle(WaitHandle, SafeWaitHandle) |
Thread Safety
This class is thread safe.