Could someone introduce an use case for AutoResetEvent.Reset() method ? When and why I would like to use this method ? I understand WaitOne and Set but this is quite unclear for me.
-
6Why a vote to close? This question seems perfectly clear and useful to me... – Dan Puzey May 03 '11 at 14:33
6 Answers
Yes the AutoResetEvent
will automatically reset it's state whenever a thread which is waiting on the event is signaled. However it's possible that a given event is no longer valid and no thread has waited on an AutoResetEvent
since it was originally set. In that scenario the Reset
method becomes useful

- 733,204
- 149
- 1,241
- 1,454
-
-
@anth it prevents any thread which hasn't yet called wait, but will at some point in the future, from being activated for an event which is no longer valid. Note: This in itself is not enough to prevent a race condition but it can be a part of a larger solution – JaredPar May 03 '11 at 15:02
Reset:
Sets the state of the event to nonsignaled ,See EventWaitHandle Class
Sample,
using System;
using System.Threading;
namespace ThreadingInCSharp.Signaling
{
class Program
{
static EventWaitHandle _waitHandle = new AutoResetEvent(false);
static void Main(string[] args)
{
//The event's state is Signal
_waitHandle.Set();
new Thread(Waiter).Start();
Thread.Sleep(2000);
_waitHandle.Set();
Console.ReadKey();
}
private static void Waiter()
{
Console.WriteLine("I'm Waiting...");
_waitHandle.WaitOne();
//The word pass will print immediately
Console.WriteLine("pass");
}
}
}
Using Reset,
using System;
using System.Threading;
namespace ThreadingInCSharp.Signaling
{
class Program
{
static EventWaitHandle _waitHandle = new AutoResetEvent(false);
static void Main(string[] args)
{
//The event's state is Signal
_waitHandle.Set();
_waitHandle.Reset();
new Thread(Waiter).Start();
Thread.Sleep(2000);
_waitHandle.Set();
Console.ReadKey();
}
private static void Waiter()
{
Console.WriteLine("I'm Waiting...");
_waitHandle.WaitOne();
//The word will wait 2 seconds for printing
Console.WriteLine("pass");
}
}
}

- 1,009
- 11
- 11
Looks like it's just inherited from EventWaitHandle. Probably more useful with the ManualResetEvent which also inherits from that class?

- 152,914
- 173
- 462
- 620
The method is inherited from the base class EventWaitHandle
and is used to (re)set an AutoResetEvent
to its "blocked" state.
Because AutoResetEvent
will automatically enter that state as soon as it's signalled, you'll typically never see this method used in code, but for other classes deriving from EventWaitHandle
it would be much more useful!

- 33,626
- 4
- 73
- 96
If the AutoResetEvent producer wants to clear the event, you would use Reset(). This way, you can safely "reset" the event without having to know if it's currently signaled. If the producer used WaitOne to "reset" it's own event, there is a risk that you could deadlock (i.e. never return since the event isn't signaled and the producer thread is blocked).

- 1,966
- 15
- 22
You should use ManualResetEvent when using Reset(), as AutoResetEvent resets itself when a thread has become signaled.

- 61,704
- 67
- 242
- 415