Removing The registered Event Handlers

M

Mustafa

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
 
K

Ken Slovak - [MVP - Outlook]

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

Mustafa

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


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



In the Above Code the Items_ItemAdd is firing ...
Regards
Mohamed Mustafa

Its not possible to release the object after setting it to false,
please correct me if I am wrong, are you trying to say
Tasks.Items.ItemAdd += new
Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);

Tasks = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(Tasks);

// Remove Event Handler
Tasks.Items.ItemAdd -= new
Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);

Is this a flaw in Outlook Object Model. I am able to Register and
Remove the Event Handlers from other UI Objects

Regards
Mohamed Mustafa
 
M

Mustafa

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

Its not possible to release the object after setting it to false,
please correct me if I am wrong, are you trying to say
Tasks.Items.ItemAdd += new
Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);

Tasks = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(Tasks);

// Remove Event Handler
Tasks.Items.ItemAdd -= new
Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);

Is this a flaw in Outlook Object Model. I am able to Register and
Remove the Event Handlers from other UI Objects

Regards
Mohamed Mustafa

ts not possible to release the object after setting it to null,
please correct me if I am wrong, are you trying to say
Tasks.Items.ItemAdd += new
Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);

Tasks = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(Tasks);

// Remove Event Handler
Tasks.Items.ItemAdd -= new
Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);

Is this a flaw in Outlook Object Model. I am able to Register and
Remove the Event Handlers from other UI Objects

Regards
Mohamed Mustafa
 
M

Mustafa

On Aug 7, 6:27 pm, "Ken Slovak - [MVP - Outlook]" <[email protected]>
wrote:
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

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
Its not possible to release the object after setting it to false,
please correct me if I am wrong, are you trying to say
Tasks.Items.ItemAdd += new
Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
Tasks = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(Tasks);
// Remove Event Handler
Tasks.Items.ItemAdd -= new
Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
Is this a flaw in Outlook Object Model. I am able to Register and
Remove the Event Handlers from other UI Objects
Regards
Mohamed Mustafa

ts not possible to release the object after setting it to null,
please correct me if I am wrong, are you trying to say
Tasks.Items.ItemAdd += new
Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);

Tasks = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(Tasks);

// Remove Event Handler
Tasks.Items.ItemAdd -= new
Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);

Is this a flaw in Outlook Object Model. I am able to Register and
Remove the Event Handlers from other UI Objects

Regards
Mohamed Mustafa

Experts, any help ....
 
K

Ken Slovak - [MVP - Outlook]

I'll answer only 1 of your 3 duplicate messages.

Now that I look at the code again I see the you're declaring Tasks at a
class level but not Items. Try declaring Items and then assigning it to
Task.Items and then add your event handler.
 
M

Mustafa

I'll answer only 1 of your 3 duplicate messages.

Now that I look at the code again I see the you're declaring Tasks at a
class level but not Items. Try declaring Items and then assigning it to
Task.Items and then add your event handler.

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


Its not possible to release the object after setting it to false,
please correct me if I am wrong, are you trying to say
Tasks.Items.ItemAdd += new
Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
Tasks = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(Tasks);
// Remove Event Handler
Tasks.Items.ItemAdd -= new
Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
Is this a flaw in Outlook Object Model. I am able to Register and
Remove the Event Handlers from other UI Objects
Regards
Mohamed Mustafa

Thanks for your reply, I tried it but again same event loop..

Regards
Mohamed Mustafa
 
K

Ken Slovak - [MVP - Outlook]

Something isn't right in the code then. Show the code as it exists now.
 
M

Mustafa

Something isn't right in the code then. Show the code as it exists now.

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


<snip>


Thanks for your reply, I tried it but again same event loop..
Regards
Mohamed Mustafa- Hide quoted text -

- Show quoted text -

Hi Ken Slovak,

Please go through my problem and see if you can help me out..


The scenario is like this, suppose you have a text box and you
registered an event Handler on TextChanged Event like


this.textBox1.TextChanged += new
System.EventHandler(this.textBox1_TextChanged);


But if I change the Text value of this TextBox inside this
TextChanged
event hanlder itself, It will create a loop, because as soon as I
change the text value, TextChange Event will fire, which will invoke
the event Handler, So to avoide this loop, inside the event handler
first i will remove the event handler on the TextChanged Event,
modify
the text value and again register the event handler as shown in the
below code...


private void textBox1_TextChanged(object sender, EventArgs e)
{
this.textBox1.TextChanged -=
this.textBox1_TextChanged; //
Remove Handler
this.textBox1.Text = System.DateTime.Now.ToString(); //
Update Data
this.textBox1.TextChanged +=
this.textBox1_TextChanged; //
Add Handler
}


this is working fine for UI objects like TextBox. But I want simulate
this functionality for Outlook Tasks Items some thing like shown
below..


using OL = Microsoft.office.Interopservices.Outlook;


class TestOutlook
{
OL.Application App = null;
OL.NameSpace Ns = null;
OL.MAPIFolder Tasks = null;


private void InitializeOutllok()
{
App = new OL.Application();
Ns = App.GetNamespace("MAPI");
Tasks =
Ns .GetDefaultFolder(OL.OlDefaultFolders.olFolderTasks);


Tasks.Items.ItemChange += Items_ItemChange; // Register Event
handler
}


private void Items_ItemChange(Object pItem)
{
OL.TaskItem UpdatedTask = null;
try
{


Tasks.Items.ItemChange -= Items_ItemChange; // Remove
Handler
OL.TaskItem UpdatedTask = (OL.TaskItem) pItem; // Update it
UpdatedTask.DueDate = System.DateTime.Now;
UpdatedTask.Save();
Tasks.Items.ItemChange += Items_ItemChange; // Add Handler
}
catch(Exception Ex)
{


}
finally
{


System.Runtime.InteropServices.Marshal.ReleaseComObject(pItem);


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



}


But I am unable to simulate this for Outlook Tasks Items(Folder)

Regards
Mohamed Mustafa
 
K

Ken Slovak - [MVP - Outlook]

You're not supposed to remove an event handler within that event handler,
you know. And given the Outlook PIA's and the Interop and things are set up
there I'm not at all surprised that you're having problems.

I had mentioned to add a declaration for an Outlook Items collection at
class level and instantiate that to the Tasks folder's Items collection.
That wasn't done. In general it's a bad practice to add an event handler
with multiple dot operators, such as:

Tasks.Items.ItemChange += Items_ItemChange;

You really should be using this sort of code:

Tasks = Ns .GetDefaultFolder(OL.OlDefaultFolders.olFolderTasks);
Items = Tasks.Items;
Items.ItemChange += Items_ItemChange; // Add Handler

Otherwise you're depending on internal object variables for your event
handler and that can cause problems with object and memory leakages and
various other things.




<snip>
Hi Ken Slovak,

Please go through my problem and see if you can help me out..


The scenario is like this, suppose you have a text box and you
registered an event Handler on TextChanged Event like


this.textBox1.TextChanged += new
System.EventHandler(this.textBox1_TextChanged);


But if I change the Text value of this TextBox inside this
TextChanged
event hanlder itself, It will create a loop, because as soon as I
change the text value, TextChange Event will fire, which will invoke
the event Handler, So to avoide this loop, inside the event handler
first i will remove the event handler on the TextChanged Event,
modify
the text value and again register the event handler as shown in the
below code...


private void textBox1_TextChanged(object sender, EventArgs e)
{
this.textBox1.TextChanged -=
this.textBox1_TextChanged; //
Remove Handler
this.textBox1.Text = System.DateTime.Now.ToString(); //
Update Data
this.textBox1.TextChanged +=
this.textBox1_TextChanged; //
Add Handler
}


this is working fine for UI objects like TextBox. But I want simulate
this functionality for Outlook Tasks Items some thing like shown
below..


using OL = Microsoft.office.Interopservices.Outlook;


class TestOutlook
{
OL.Application App = null;
OL.NameSpace Ns = null;
OL.MAPIFolder Tasks = null;


private void InitializeOutllok()
{
App = new OL.Application();
Ns = App.GetNamespace("MAPI");
Tasks =
Ns .GetDefaultFolder(OL.OlDefaultFolders.olFolderTasks);


Tasks.Items.ItemChange += Items_ItemChange; // Register Event
handler
}


private void Items_ItemChange(Object pItem)
{
OL.TaskItem UpdatedTask = null;
try
{


Tasks.Items.ItemChange -= Items_ItemChange; // Remove
Handler
OL.TaskItem UpdatedTask = (OL.TaskItem) pItem; // Update it
UpdatedTask.DueDate = System.DateTime.Now;
UpdatedTask.Save();
Tasks.Items.ItemChange += Items_ItemChange; // Add Handler
}
catch(Exception Ex)
{


}
finally
{


System.Runtime.InteropServices.Marshal.ReleaseComObject(pItem);


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



}


But I am unable to simulate this for Outlook Tasks Items(Folder)

Regards
Mohamed Mustafa
 

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