Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Outlook / Programming Add-Ins / November 2006

Tip: Looking for answers? Try searching our database.

Unsubscribing from events (VSTO, C#)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
pavel.gz@gmail.com - 20 Nov 2006 13:42 GMT
In my add-in I have a method that unsubcribes from delete event
handler(it was previously subscribed to) in the begining and subscribes
to it again at the end like:

public void SynchronizeTasks(...)
       {
           tasksItems.ItemRemove -= OnTasksDelete;

           ....
           foreach (Outlook.TaskItem task in tasksItems)
           {
               task.Delete();
           }
           ....

           tasksItems.ItemRemove += new
Outlook.ItemsEvents_ItemRemoveEventHandler(OnTasksDelete);
       }

However, for every task deleted in this method I catch an ItemRemove
event in OnTasksDelete. Is there any way to completely unsubscribe from
this event?

Thanks in advance.
Ken Slovak - [MVP - Outlook] - 20 Nov 2006 15:29 GMT
See if it helps if your code unsubscribes like this:

tasksItems.ItemRemove -= new
Outlook.ItemsEvents_ItemRemoveEventHandler(OnTasksDelete);

If that doesn't help you might have to call the garbage collector and wait
for finality before you proceed with your deletion code.

You also should use a count down for loop or other type of loop where you
check for the count of items rather than a foreach loop. The count is
essentially being decremented within the loop as you delete items in a count
up for loop or foreach loop and that messes with things. Often you only get
every other item deleted.

Signature

Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm

> In my add-in I have a method that unsubcribes from delete event
> handler(it was previously subscribed to) in the begining and subscribes
[quoted text clipped - 20 lines]
>
> Thanks in advance.
pavel.gz@gmail.com - 21 Nov 2006 08:51 GMT
Ken, thanks for your suggestion, but it didn't work. I've changed the
unsubscription code and I've implemented a call of GC.Collect(); right
after unsubscription and just before subscription. Besides, I've
changed iteration through the collection to iteration through the
EntryIDs (I have them saved in my add-in). But the handler keeps
catching an event on every item deleted. It appears I'll have to check
in handler that event wasn't caught right after my cleaning...

"""
> See if it helps if your code unsubscribes like this:
>
[quoted text clipped - 42 lines]
> >
> > Thanks in advance.
Ken Slovak - [MVP - Outlook] - 21 Nov 2006 14:08 GMT
Did you just call the GC or did you do that and wait for finalization of
garbage collection? You might also have to release the COM object that has
the event handler before GC and instantiate a new object. I'd probably try
that as a belt and suspenders test to see if that works.

System.Runtime.InteropServices.Marshal.ReleaseComObject(tasksItems);
tasksItems = null;
GC.Collect();
GC.WaitForPendingFinalizers();

Then re-instantiate the tasksItems collection. See if that helps.

Signature

Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm

> Ken, thanks for your suggestion, but it didn't work. I've changed the
> unsubscription code and I've implemented a call of GC.Collect(); right
[quoted text clipped - 3 lines]
> catching an event on every item deleted. It appears I'll have to check
> in handler that event wasn't caught right after my cleaning...
pavel.gz@gmail.com - 22 Nov 2006 08:05 GMT
Ken, thanks a lot, that was it! After adding

> System.Runtime.InteropServices.Marshal.ReleaseComObject(tasksItems);
> tasksItems = null;
> GC.Collect();

after unsubscription, Handler finally began no to catch delete event.
There was a problem when I also add GC.WaitForPendingFinalizers(); -
after executing this string outlook just stops responding, but without
it everything works just great.

"""
> Did you just call the GC or did you do that and wait for finalization of
> garbage collection? You might also have to release the COM object that has
[quoted text clipped - 23 lines]
> > catching an event on every item deleted. It appears I'll have to check
> > in handler that event wasn't caught right after my cleaning...
Ken Slovak - [MVP - Outlook] - 22 Nov 2006 14:49 GMT
Good.

The problem with the GC is that's it's non-deterministic. So you can release
something but the garbage collector might not get around to actually
releasing the references and cleaning things up for a while. In computer
code that seems like forever. Forcing collection releases things and gets
rid of those pesky event handlers.

Signature

Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm

> Ken, thanks a lot, that was it! After adding
>
[quoted text clipped - 6 lines]
> after executing this string outlook just stops responding, but without
> it everything works just great.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.