开发者

Must scenario for events related to Multithreading in C#?

开发者 https://www.devze.com 2023-01-19 11:09 出处:网络
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

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 imple开发者_JS百科ment this.

Can anyone help me out to understand events in Multithreading ?


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".


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消