Event ItemAdd in outlook add-in stops responding

A

Ahmad Atwi

Hi,

I have created an outlook add-in that has a listener running on a public
folder. On a new email added, the application parses the email and then adds
an instance of it in the database and finally moves it to another folder
based on some criterions that we have predefined.
my problem is that the event handler stops working after receiving 8 or 9
emails thus the emails remain in the public folder untill the application is
restarted.
I know that the error is with the event handler because the rest of the
application is running perfectly for now (a Timer and 2 Buttons).

Note: I am working on VS2003, .net Framework 1.1 and Outlook 2003
I can provide more details if requested.

Thanks for your help
 
K

Ken Slovak - [MVP - Outlook]

That sounds like the event handler is being garbage collected and therefore
no longer has any scope or existence.

You need to declare an instance of the email item you want to handle at a
class level that will maintain scope for the life of that email item. That
should keep your event handler alive.
 
A

Ahmad Atwi

Thanks for the reply Ken,

I am not sure if that is the problem, because I already have the instances
of the FOLDERS declared at the class level, as for the instance of the email
item is sent throught the parameter of event handler so it should not be
collected by the garbage collection at that time.

--
Best Regards
Ahmad Atwi


Ken Slovak - said:
That sounds like the event handler is being garbage collected and therefore
no longer has any scope or existence.

You need to declare an instance of the email item you want to handle at a
class level that will maintain scope for the life of that email item. That
should keep your event handler alive.
 
K

Ken Slovak - [MVP - Outlook]

Folders does nothing for you in this case. If you don't have a declared
object variable for each mail item you want to handle the event will be
garbage collected and not be usable any longer.
 
A

Ahmad Atwi

The reason I declared the folders at class level is that the event handlers
are added on them so my code would be
"this.base_folder.Items.ItemAdd += new
ItemsEvents_ItemAddEventHandler(base_folder_itemAdd);"
in my application i dont need to add event handlers at the email item, that
is why I didnot understand why or how to declare the email item at class
level.

i really appreciate your help, i have been working on this for couple of
days and I think it is causing other problems to occur.
--
Best Regards
Ahmad Atwi


Ken Slovak - said:
Folders does nothing for you in this case. If you don't have a declared
object variable for each mail item you want to handle the event will be
garbage collected and not be usable any longer.
 
K

Ken Slovak - [MVP - Outlook]

In the case you mention the Folders are not handling the event, it's the
Items collection of the folder that is. So when you say
this.base_folder.Items.ItemAdd you are creating an implicit Items variable
in that scope and when the scope changes that implicit Items collection will
be garbage collected. It may not happen right away, but it will happen.

Now if you declare this at class level:

Outlook.Items items = null;

Then when you instantiate it as

items = this.base_folder.Items;

that "items" will not be garbage collected.

In fact, using dot operators such as folder.Items.ItemAdd or
folder.Items.Count creates those implicit variables that you can't release
explicitly and that can cause all sorts of problems. You should take care to
always use at most 1 dot operator for any COM objects to avoid the problem.
 
A

Ahmad Atwi

Thanks for the help Ken.

For now everything is working fine.
--
Best Regards
Ahmad Atwi


Ken Slovak - said:
In the case you mention the Folders are not handling the event, it's the
Items collection of the folder that is. So when you say
this.base_folder.Items.ItemAdd you are creating an implicit Items variable
in that scope and when the scope changes that implicit Items collection will
be garbage collected. It may not happen right away, but it will happen.

Now if you declare this at class level:

Outlook.Items items = null;

Then when you instantiate it as

items = this.base_folder.Items;

that "items" will not be garbage collected.

In fact, using dot operators such as folder.Items.ItemAdd or
folder.Items.Count creates those implicit variables that you can't release
explicitly and that can cause all sorts of problems. You should take care to
always use at most 1 dot operator for any COM objects to avoid the problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top