对Exchange中的公用文件夹ItemAdd事件

对Exchange中的公用文件夹ItemAdd事件

本文介绍了对Exchange中的公用文件夹ItemAdd事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有附加的事件处理程序公用文件夹的 ItemAdd 事件的一个问题。



The code is trivial. I have a ThisAddIn class which creates an object which in turn attaches a function to the ItemAdd event in its constructor. The function just pops up a messagebox.

Please point me in the right direction. I simply don't understand where to look for the error.

Thank you in advance,Anatoly

Here is the test code I try to run:

public partial class ThisAddIn
{
    internal static Outlook.Folder posts_folder = null;
    private static test t;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {

        t = new test();
    }
{

class test
{
    public test()
    {
        System.Windows.Forms.MessageBox.Show("Attaching...");
        ThisAddIn.posts_folder.Items.ItemAdd +=new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
    }
    void Items_ItemAdd(object Item)
    {
        System.Windows.Forms.MessageBox.Show((Item as Outlook.PostItem).Subject);
    }
}
解决方案

Continuous googling did its job. I found how to resolve the issue. It appears I am not the only one who experience it.

I added the reference to Items collection of the folder I want to track to the global scope:

internal static class stor
{
    public static Outlook.Items i;
}

public partial class ThisAddIn
{
    internal static Outlook.Folder posts_folder = null;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        // the code for finding a posts_folder is omitted

        stor.i = posts_folder.Items;
        stor.i.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(Posts_Add);
    }

    static void Posts_Add(object Item)
    {
        System.Windows.Forms.MessageBox.Show("New item");
    }
{

Now it works as expected. Alhough I do not understand all the details they say it is a garbage collection issue. My event handler eventually was thrown into the garbage. The reference to the Items collection on the global scope prevents this from happening.

这篇关于对Exchange中的公用文件夹ItemAdd事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 02:55