Re: Removing The registered Event Handlers



Try setting both the Tasks and event to null and then call Marshal.ReleaseComObject on both. See if that helps.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Mustafa" <mustafa.faisan@xxxxxxxxx> wrote in message news:e2b194f1-45d4-4bbd-a825-f97a7c79fffb@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi all,

I am writing a Outlook Add-in which subscribes to the Event in
Outlook, I am registering an Event Handler for Task Folder (See code
Below), But later I want to Remove this Handler from Task Folder so
that this function is not Executed. But the Removing the Event Handler
is not Working, the event is getting fired even after removing the
Event Handler, I appreciate any Help and Suggestions

class test
{
Outlook.Application App = null;
Outlook.NameSpace Ns = null;
Outlook.MAPIFolder Tasks = null;
Outlook.ItemsEvents_ItemAddEventHandler ItemAdd = null;
Outlook.TaskItem FirstTask = null;

private void Initialize_Outlook()
{
try
{
App = new Outlook.Application();
Ns = App.GetNamespace("MAPI");
Tasks =
Ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);

// Register Event Handler
Tasks.Items.ItemAdd += new
Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);

// Remove Event Handler
Tasks.Items.ItemAdd -= new
Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
}
catch (Exception Ex)
{
MessageBox.Show("Outlook Exception : " + Ex.Message);
}
finally
{

}
}

void Items_ItemAdd(object Item)
{
try
{
MessageBox.Show("From Task Add
event");
}
catch (Exception Ex)
{

}
finally
{

System.Runtime.InteropServices.Marshal.ReleaseComObject(Item);
}
}

}

In the Above Code the Items_ItemAdd is firing ...

Regards
Mohamed Mustafa

.



Relevant Pages

  • Re: Throwing Exceptions
    ... I just ran it and got the same result, it does not throw the exception! ... > private void InitializeComponent() ... > private void lblDest_DragDrop(object sender, ... >>> Create DragDrop event handler for label. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: new inspector not firing
    ... Your problem has the exact same cause, you aren't declaring an Inspectors collection object at class level and adding the event handler to that object. ... [MVP - Outlook] ... public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom) ...
    (microsoft.public.office.developer.com.add_ins)
  • Re: whats the best practice to do the garbage collection?
    ... The Quit event is pretty useless. ... What is needed is an event handler for Explorer.Close and one for Inspector.Close. ... Ken Slovak [MVP - Outlook] http://www.slovaktech.com ... will do the garbage collection automatically but in an undeteministic way, ...
    (microsoft.public.outlook.program_vba)
  • Re: New Macro / VBA
    ... Author of Microsoft Outlook 2007 Programming: ... Application_ItemSend event handler there, you'll need to combine the two ... Opened the vba outlook window, which was allready populated with 'this ... However, if your new code is not a macro but an event handler, put it ...
    (microsoft.public.outlook.program_vba)
  • Re: Address Book
    ... AddQuote() can be anywhere but needs to be Public if its in another module. ... Sue Mosher, Outlook MVP ... of code proper. ... The Application_ItemSend event handler must be in the ...
    (microsoft.public.outlook.contacts)