Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoResetEvent Reset method

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.

like image 981
anth Avatar asked May 03 '11 14:05

anth


People also ask

How do I use auto reset event?

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.

What is AutoResetEvent and how it is different from ManualResetEvent?

An AutoResetEvent resets when the code passes through event. WaitOne() , but a ManualResetEvent does not.

What is ManualResetEvent in C#?

Set to signal that the waiting threads can proceed. All waiting threads are released. Once it has been signaled, ManualResetEvent remains signaled until it is manually reset by calling the Reset() method. That is, calls to WaitOne return immediately.


3 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

like image 86
JaredPar Avatar answered Sep 24 '22 01:09

JaredPar


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

like image 44
Svish Avatar answered Sep 24 '22 01:09

Svish


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!

like image 43
Dan Puzey Avatar answered Sep 27 '22 01:09

Dan Puzey