Link to home
Start Free TrialLog in
Avatar of gorlaz
gorlazFlag for Australia

asked on

event handler problems c#

Hi,

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.
Avatar of ripahoratiu
ripahoratiu
Flag of Romania image

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.
Avatar of gorlaz

ASKER

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

Avatar of gorlaz

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of ripahoratiu
ripahoratiu
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gorlaz

ASKER

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!
Avatar of gorlaz

ASKER

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.
Avatar of gorlaz

ASKER

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!!