views:

8511

answers:

5

I have read the docs on this and I think I understand. Auto resets when the code passes through event.WaitOne() but manual does not. Is this correct?

+7  A: 

The short answer is yes. The most important difference is that an AutoResetEvent will only allow one single waiting thread to continue. A ManualResetEvent on the other hand will keep allowing threads, several at the same time even, to continue until you tell it to stop (Reset it).

Martin Brown
+46  A: 

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

Dan Goldstein
That is a great analogy.
twk
@Dan very good understanding.
balaweblog
Even worse, do not wait to long from setting the ARE to the WaitOne, or it will be resetted in the meantime. Had many abandoned threads with that.
BeowulfOF
Or like a door and a turnstile.
Constantin
tollbooth and a door analogy...too good.
P.K
+3  A: 

Yes. This is absolutely correct.

You could see ManualResetEvent as a way to indicate state. Something is on (Set) or off (Reset). An occurrence with some duration. Any thread waiting for that state to happen can proceed.

An AutoResetEvent is more comparable to a signal. A one shot indication that something has happened. An occurrence without any duration. Typically but not necessarily the "something" that has happened is small and needs to be handled by a single thread - hence the automatic reset after a single thread have consumed the event.

Boaz
+2  A: 

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

Michael Damatov
+3  A: 

Taken from C# 3.0 Nutshell book, by Joseph Albahari

Threading in C# - Free E-Book

A ManualResetEvent is a variation on AutoResetEvent. It differs in that it doesn't automatically reset after a thread is let through on a WaitOne call, and so functions like a gate: calling Set opens the gate, allowing any number of threads that WaitOne at the gate through; calling Reset closes the gate, causing, potentially, a queue of waiters to accumulate until its next opened.

One could simulate this functionality with a boolean "gateOpen" field (declared with the volatile keyword) in combination with "spin-sleeping" – repeatedly checking the flag, and then sleeping for a short period of time.

ManualResetEvents are sometimes used to signal that a particular operation is complete, or that a thread's completed initialization and is ready to perform work.