Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 557
  • Last Modified:

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.
0
gorlaz
Asked:
gorlaz
  • 5
  • 2
1 Solution
 
ripahoratiuCommented:
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.
0
 
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

0
 
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?
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

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

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now