Posts

Showing posts from January, 2013

Events and Multithreading

This is  the second and final part of this series. Will focus now a bit on what happen when we add multithreading to our work with events. Cleaning Up Events and Context Information In the previous post I mentioned some issues using lambdas and anonymous methods on events. The unsubscribe problem had a tricky "solution" by declaring a "cleanup" method inside the class that holds the event. public void CleanupSubscribers() { SayHelloCompleted = null; } The problem here is obvious since the cleanup method will remove all subscribers from the event. Hence, individually removing listeners from the event gets tricky when using lambdas/anonymous methods. So in this case the recommendations would be: Do not use lambdas if you need to unsubscribe. If you do so, use the cleanup method UNLESS your code is multithreaded.  NOTE: this only applies to the use of lambda expressions and anonymous methods, as for regular named methods you can always r