event handler problems c#


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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
gorlazAuthor Commented:
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

gorlazAuthor Commented:
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?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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();

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gorlazAuthor Commented:
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!
gorlazAuthor Commented:
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.
gorlazAuthor Commented:
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!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.