A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Add-ins for Outlook
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Unsubscribing from events (VSTO, C#)



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old November 20th 06, 01:42 PM posted to microsoft.public.outlook.program_addins
[email protected]
external usenet poster
 
Posts: 6
Default Unsubscribing from events (VSTO, C#)

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(OnTasks Delete);
}

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.

Ads
  #2  
Old November 20th 06, 03:29 PM posted to microsoft.public.outlook.program_addins
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Unsubscribing from events (VSTO, C#)

See if it helps if your code unsubscribes like this:

tasksItems.ItemRemove -= new
Outlook.ItemsEvents_ItemRemoveEventHandler(OnTasks Delete);

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.


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


wrote in message
oups.com...
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(OnTasks Delete);
}

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.


  #3  
Old November 21st 06, 08:51 AM posted to microsoft.public.outlook.program_addins
[email protected]
external usenet poster
 
Posts: 6
Default Unsubscribing from events (VSTO, C#)

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

"""Ken Slovak - [MVP - Outlook] ΠΙΣΑΜ(Α):
"""
See if it helps if your code unsubscribes like this:

tasksItems.ItemRemove -= new
Outlook.ItemsEvents_ItemRemoveEventHandler(OnTasks Delete);

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.


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


wrote in message
oups.com...
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(OnTasks Delete);
}

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.


  #4  
Old November 21st 06, 02:08 PM posted to microsoft.public.outlook.program_addins
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Unsubscribing from events (VSTO, C#)

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.ReleaseComO bject(tasksItems);
tasksItems = null;
GC.Collect();
GC.WaitForPendingFinalizers();

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

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


wrote in message
oups.com...
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...


  #5  
Old November 22nd 06, 08:05 AM posted to microsoft.public.outlook.program_addins
[email protected]
external usenet poster
 
Posts: 6
Default Unsubscribing from events (VSTO, C#)

Ken, thanks a lot, that was it! After adding

System.Runtime.InteropServices.Marshal.ReleaseComO bject(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.

"""Ken Slovak - [MVP - Outlook] ΠΙΣΑΜ(Α):
"""
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.ReleaseComO bject(tasksItems);
tasksItems = null;
GC.Collect();
GC.WaitForPendingFinalizers();

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

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


wrote in message
oups.com...
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...


  #6  
Old November 22nd 06, 02:49 PM posted to microsoft.public.outlook.program_addins
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Unsubscribing from events (VSTO, C#)

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.

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


wrote in message
oups.com...
Ken, thanks a lot, that was it! After adding

System.Runtime.InteropServices.Marshal.ReleaseComO bject(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.


 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Outlook 2003 and VSTO questions Christie Add-ins for Outlook 0 November 3rd 06 07:19 AM
VSTO Item events add and remove ... lg Add-ins for Outlook 0 October 12th 06 03:01 PM
Item open events and double click events in exchange client extension. Fanxa Add-ins for Outlook 1 August 9th 06 08:18 AM
RemoteCalendars finally available with VSTO technology [email protected] Outlook - General Queries 0 April 4th 06 04:51 PM
Handling calendar items in VSTO jirina42 Outlook - Calandaring 1 March 20th 06 08:47 PM


All times are GMT +1. The time now is 08:40 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.