views:

1942

answers:

3

They both seem to fulfil the same purpose. When would I chose one over the other?

+3  A: 

Use the events when you've got a thread that is waiting on one of or all of a number of events to do something.

Use the monitor if you want to restrict access to a data structure by limiting how many threads can access it.

Monitors usually protect a resource, whereas events tell you something's happening, like the application shutting down.

Also, events can be named (see the OpenExisting method), this allows them to be used for synchronization across different processes.

Scott Langham
+1  A: 

This tutorial has detailed descriptions of what you'll need to know: http://www.albahari.com/threading/

In particular, this will cover the XXXResetEvent classes,
http://www.albahari.com/threading/part2.aspx

and this will cover Wait/Pulse : http://www.albahari.com/threading/part4.aspx#_Wait_and_Pulse

torial
Thanks for the url. It was very useful.
+1  A: 

In my opinion, it's better to use Monitor if you can, Monitor.Wait and Monitor.Pulse/PulseAll are used for signalling between threads (as are Manual/AutoResetEvent) however Monitor is quicker, and doesn't use a native system resource. Also apparently Monitor is implemented in user mode and is managed, whereas Manual/AutoResetEvents require switching to kernel mode and p/invoke out to native win32 calls that use a wait handle.

There are situations where you would need to use Manual/AutoResetEvent, for example, to signal between processes you can use named events, and I guess to signal native threads in your app.

I am just regurgitating what I have read in this excellent article about threading:

http://www.yoda.arachsys.com/csharp/threads/waithandles.shtml

The whole article is worth reading, however the link takes you to the wait handle section that details the events and monitor wait/pulse.

Matt

Matt