event handler problems c#

Posted on 2008-11-10
Last Modified: 2013-11-07

I am having some trouble keeping an event handle alive for some reason.

My project is an outlook addin which prompts the user to do something when they send an email. Now, I have an event handler hooked up to the Sent Items folder so that when the sent email arrives in that folder it fires the prompt. The reason I have it attached to the sent items folder is so that I can deal with the email after it is sent.

Initially all worked great and then I put it onto a client machine and the event didnt want to fire at all. This is when I attached another event (initially to figure what was happening) to the onSend event. What this did was check to see, via a global flag, if the handle had been placed on the Sent Items folder. If it had not it would create it and switch the flag. The global flag was required so that the handle was not created multiple times every time another email was sent.

I hope this is all making sense!

What is happening now though is in some instances the first few emails will respond how they are supposed to; ie prompt after they have been sent. after this it stops to prompt and i cant figure out why.

I am hopeing that maybe there is a way I can check to see if the event handle exists directly rather than using a global flag, as i dont like this method anyhow. My only other thought is that maybe the handle i have setup is getting cleaned up somehow?

If anyone has any suggestions that help me figure what is going on i would greatly appreciate it.
Question by:gorlaz
    LVL 9

    Expert Comment

    Well, an event handler has the same scope with the object within it resides and probably, by some means, your object is destroyed.
    If you want to have only one instance, the better approach is a singleton, that ensures you two things:
    1. only one instance  
    2. application domain scope of your object (since the inner object is static)

    Another explanation may be (but I cant tell that without code) some sort of unsubscription of the event from the handler.

    Author Comment

    thanks for your thoughts ... i will give the singleton idea a go and see if that helps at all.
    Let me just make sure I am thinking on the right lines here though; if i create the handle with the singleton approach i shouldnt even need to use the global flag? and also if i attempt to call it after it has already been created it will obviously not create a second instance? but if for some reason the original instance has died it will create a new one?

    i have attached the code where the handle is created (prior to singleton), as simple as you get really.
    if (!sentItemsListenerLoaded) {
    	try {
    		Outlook.MAPIFolder _sentMailFolder = _outlookApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
    		Outlook.Items _sentMailItems = _sentMailFolder.Items;
    		_sentMailItems.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(MyItemSentEventHandler);
    		sentItemsListenerLoaded = true; // flag to stop it being handled multiple times
    	} catch (Exception e) {
    		dmsError(e, "Exception on SentItems Listener Creation ::\n\t"); // handle if error occurs

    Open in new window


    Author Comment

    ok. i think i got the singleton bit ok, simple but it seems to work. (so far)
    I have only created it with a simple alert which will tell me when it has fired so i could test it out, and it appears to be good.

    public class SentItemsHandle {
          protected SentItemsHandle() {
                MessageBox.Show("sent item handle instance created here!");
          public static readonly SentItemsHandle handle = new SentItemsHandle();

    and i call it as such ...
    SentItemsHandle testhandle = SentItemsHandle.handle;

    only thing now is to get the code above into the singleton constructor. the problem i see though is i need to somehow get access to the _outlookApp object so that i can attach the event handler correctly to the instance of Outlook.

    is this correct of am i missing the point?
    LVL 9

    Accepted Solution

    if i create the handle with the singleton approach i shouldnt even need to use the global flag? No
    if i attempt to call it after it has already been created it will obviously not create a second instance? No, it won't create another instance
    if for some reason the original instance has died it will create a new one? if the original instance becomes null it will but the singleton should look like:

     public class SentItemsHandle
            private static SentItemsHandle handle = null;
            private static object lockObject = new object();
            private SentItemsHandle()
                // ... some other initializing code here the applies on handle object
            public static SentItemsHandle GetInstance()
                lock (lockObject) // for multithreading environments
                    if (handle == null)
                        handle = new SentItemsHandle();
                return handle;

    and you'll get a handle through
    SentItemsHandle testhandle = SentItemsHandle.GetInstance();


    Author Comment

    That is awesome. Thank you for your help.

    I have implemented the Singleton and it appears to work perfectly. I just hope it stops the event handle from falling off :)

    Thanks again!

    Author Closing Comment

    cheers :)
    if i find the event handle starts to fall off again I will try again but this should do the job and is working great thus far.

    Author Comment

    I may actually need to start another thread now. As mentioned the Singleton class works well but it has not actually stopped the handle dropping off. So for whatever reason I still have the same problem.
    Somewhere, somehow, sometime the event just stops being handled!!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Find out how to use dynamic social media in email signatures with this top 10 DOs & DON’Ts.
    Outlook Free & Paid Tools
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
    The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now