Event handler Loop in Outlook Folder Events

M

Mustafa

Hi all,

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
 
D

Dmitry Streblechenko

Use a global (class) boolean variable that will indicate that you are
already processing the event.

boolean bSkipHandler = false;
private void Items_ItemChange(Object pItem)
{
if (!bSkipHandler)
{
bSkipHandler = true;
try
{
...
}
finally
{
bSkipHandler = false;
}
}
}

--
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
-
 
M

Mustafa

Use a global (class) boolean variable that will indicate that you are
already processing the event.

boolean bSkipHandler = false;
private void Items_ItemChange(Object pItem)
{
if (!bSkipHandler)
{
bSkipHandler = true;
try
{
...
}
finally
{
bSkipHandler = false;
}
}

}


Hi Dmitry Streblechenko.

Thanks for ur reply but its not working, the Event is getting fired
again which is creating a loop, this is very strange behavior from
Outlook Object Model..


Regards
Mohamed Mustafa
 
D

Dmitry Streblechenko

If the event fires asynchrnously (which is the case for the Items
collection), save the value of the LastModificationTime immediately after
you call Save. When the event (caused by your change) fires again, compare
the value of LastModificationTime with the stored value to see if the change
is more recent. If not, ignore it.
Also, why do you blindly modify the items multiple times? Why not check the
existing values of whatever properties you set to see if they are already
set to the required values and avoid modifying the item again?

--
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
-
 

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