Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ManualResetEvent and AutoResetEvent in .NET?

I have read the documentation on this and I think I understand. An AutoResetEvent resets when the code passes through event.WaitOne(), but a ManualResetEvent does not.

Is this correct?

like image 527
Ben McNiel Avatar asked Sep 30 '08 16:09

Ben McNiel


People also ask

What is ManualResetEvent?

ManualResetEvent is used for send signals between two or more threads. Multiple threads can enter into a waiting/blocking state by calling the WaitOne method on ManualResetEvent object. When controlling thread calls the Set method all the waiting threads are unblocked and free to proceed.

How do I use ManualResetEvent?

Start the main thread; When an asynchronous worker thread is triggered, call the ManualResetEvent object's WaitOne() method to block the main thread; When the worker thread has completed, call the ManualResetEvent object's Set() method to release the main thread and allow it to continue.


2 Answers

Yes. It's like the difference between a tollbooth and a door. The ManualResetEvent is the door, which needs to be closed (reset) manually. The AutoResetEvent is a tollbooth, allowing one car to go by and automatically closing before the next one can get through.

like image 103
Dan Goldstein Avatar answered Oct 13 '22 08:10

Dan Goldstein


Just imagine that the AutoResetEvent executes WaitOne() and Reset() as a single atomic operation.

like image 23
Michael Damatov Avatar answered Oct 13 '22 08:10

Michael Damatov