views:

55

answers:

3

I am wondering that in which scenario we must use events technique in multithreading and there is no other way around? I have seen cases we may use another syncronization techniques like Monitor wait and pulse to implement this.

Can anyone help me out to understand events in Multithreading ?

+2  A: 

Multithreaded Programming with the Event-based Asynchronous Pattern

The above article should give you some excellent resources on how to use events in a Multi-threaded environment.

James
+3  A: 

There are none. You can always handle it at a lower level of abstraction (until you're pushing around 1s and 0s, you can always handle anything at a lower level of abstraction). However, it is often cleaner and/or simpler to do so. http://msdn.microsoft.com/en-us/library/ms228966.aspx gives advice on the matter, but there are no "must scenarios".

Jon Hanna
+2  A: 

I would support @Jon Hanna's answer but emphasize the fact that .Net events implement a commonly-used pattern (Observer) with sufficient elegance that it is sheer perversity in most instances to implement it any other way.

This is demonstrated by the way events turn up in the .Net Framework classes themselves, not just as a language construct for application programmers to play with. They are a first-class language feature, and as such to consciously avoid using them does not make sense. It would be like avoiding foreach loops just because there are other ways to achieve the same end.

See here for an overview of Observer pattern and how events address this in .Net. It's unlikely that anything you build using other .Net programming methods would match the simplicity and expressiveness of the event implementation.

Steve Townsend
+1 but as a small caveat, there *are* reasons to avoid a `foreach` in some very specific scenarios.
Winston Smith
Yep, my including the link to the MSDN advice is for much the same reason; I think we differ only in emphasis.
Jon Hanna