Click here to Skip to main content
13,552,926 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 20 Jul 2010
Licenced CPOL

Tips & Tricks - Working with Event Handler

, 20 Jul 2010
Rate this:
Please Sign up or sign in to vote.
Working with Event Handler

Sometimes, we write the following code while working with EventHandler and that may create some problems. I will discuss this later on. Let us see the code:

private void RaiseOperationComplete(EventArgs<string> e)
    if(OperationComplete != null)
        OperationComplete(this, e);

In the above case, suppose two threads subscribed to the same OperationComplete handler and one unsubscribed the event. In such a scenario, the OperationComplete event will become null and for the next thread since the event is null, it will not execute. This happens mainly in multithreaded applications, but you can't guarantee for single threaded applications too.

So what do we do? How can we make sure that our application will not come into such a situation and work properly?

Yup, let us modify the code a bit. First, we will store the event handler to a local variable and then we will raise the event from the locally cached copy. Have a look into the following code:

private void RaiseOperationComplete(EventArgs<string> e)
    EventHandler<string<eventargs><string>> handler = OperationComplete;
    if(handler != null)
        handler(this, e);

Here, when we execute the OperationComplete event handler, we will store the current value in a local variable named "handler". Hence, both "handler" and "OperationComplete" will point to the same object. Thus, we will create a cached copy of the same value for the handler and then raise the event.

The reason you do this is because the assignment is a copy of the delegate instance at that point, and not a by-value copy. Doing this, the reference will be cached just in case another thread, or re-entrant code, nulls out the reference before you call it. The original value will not change though it will set the event to null.

So, remember to always follow the second approach and be in the safer place of coding.
Thanks to Pete O'Hanlon for pointing this to me in this post.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kunal Chowdhury «IN»
Software Developer (Senior)
India India
Kunal Chowdhury is a Microsoft "Windows Platform Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik Developer Expert, Nokia Developer Champion, Windows 10 Champion, Microsoft Rockstar, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows Store (WinRT) app development. He posts his findings, articles, tutorials in his technical blog and CodeProject.

Book authored:

Connect with me on:

My CodeProject Awards/Prizes:

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180515.1 | Last Updated 20 Jul 2010
Article Copyright 2010 by Kunal Chowdhury «IN»
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid