I'm working on a WPF application that monitors numerous folders in an Outlook Shared Mailbox. I have wired up ItemAdd and ItemRemove event handlers to a Folder.Items object.
Everything works great for a few minutes. But as time goes on, the event handling seems to go "poof". Some folders will still recognize add and remove, others will only see removes, while others are blind to any activity. To me it seems like the event handlers are being garbage collected, but my Items object IS declared as a global variable in the class it sits in, so I don't see how they could be GC'd out.
Are there any pitfalls I should be aware of with Outlook Folder.Items events? I have a previous, simpler application that works by similar processes that works fine for extended periods of time. There is no intrinsic difference, as far as Item event handling goes, between my old app and this new one. I'm really at a loss as to what's causing this.
Below is the relevant code. To bring some context to this, what I'm doing is, for each Folder in the Outlook Shared Mailbox a "TicketView" UserControl is created which represents the contents (MailItems) of that folder. This TicketView is a simple ListBox that may contain between 0 to a couple dozen MailItems -- nothing excessive.
public partial class TicketView : UserControl
{
private Folder _thisFolder = null;
private TicketCollection _thisTicketColl = null;
private Items _thisItems = null;
public TicketView(Folder folder)
{
InitializeComponent();
_thisTicketColl = this.FindResource("TicketCollection") as TicketCollection;
_thisFolder = folder;
_thisItems = folder.Items;
SetFolderEvents();
Refresh();
}
private void SetFolderEvents()
{
_thisItems.ItemAdd += new ItemsEvents_ItemAddEventHandler(delegate
{
Refresh();
});
_thisItems.ItemRemove += new ItemsEvents_ItemRemoveEventHandler(delegate
{
Refresh();
});
}
public void Refresh()
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(delegate(object sender, DoWorkEventArgs e)
{
string[] fields = new string[] { "Subject", "SenderName", "SentOn", "EntryID" };
var olTable = TicketMonitorStatics.GetOutlookTable(_thisFolder, fields, filter);
olTable.Sort("SentOn", true);
var refreshedList = new List<Ticket>();
while (!olTable.EndOfTable)
{
var olRow = olTable.GetNextRow();
refreshedList.Add(new Ticket
{
Subject = olRow["Subject"],
Sender = olRow["SenderName"],
SentOn = olRow["SentOn"],
EntryID = olRow["EntryID"]
});
};
e.Result = refreshedList;
});
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object sender, RunWorkerCompletedEventArgs e)
{
var refreshedList = e.Result as List<Ticket>;
UpdateTicketList(refreshedList);
worker.Dispose();
});
worker.RunWorkerAsync();
}
private void UpdateTicketList(List<Ticket> newList)
{
_thisTicketColl.Clear();
foreach (Ticket t in newList)
{
_thisTicketColl.Add(t);
}
}
}
}
Outlook events should not be used for any kind of synchronization. They are designed to be used for the UI purposes only and can be dropped under heavy loads or if a network error occurs (if you are using an online store).
You can use events only as a hint that your code needs to run sooner rather than later.
You can use the IExchangeExportChanges
MAPI interface (C++ or Delphi only) to perform synchronization; this is the same API used by Outlook to synchronize its cached folders. If you are not using C++ or Delphi, you can use Redemption (I am its author) and its RDOFolderSynchronizer object.