在C#中两次调用Outlook的约会项更改事件 [英] appointment item change event of outlook called 2 times in c#

查看:107
本文介绍了在C#中两次调用Outlook的约会项更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Outlook加载项,因此必须跟踪约会项的添加,更改和更新;删除事件. 所以我在使用波纹管代码.

   public Microsoft.Office.Interop.Outlook.Application app = null;
   public Outlook.NameSpace ns = null;
   public Outlook.MAPIFolder calendar = null;
   public Outlook.Items appointments = null;

private void ThisAddIn_Startup(object sender, System.EventArgs e) 
{
    app = Application;
    ns = app.GetNamespace("mapi");
    ns.Logon("", "", true, true);
    calendar = ns.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
    appointments = calendar.Items;

    appointments.ItemAdd += Item_Add;
    appointments.ItemChange += Item_Change;
    appointments.ItemRemove += Item_Remove;
}

private void Item_Add(object item) 
{
   // some logic
}

private void Item_Change(object item) 
{
  // some logic
}

private void Item_Remove() 
{
  // some logic
}

现在当我在那时添加会议时Item_Add事件被调用.

当我更新创建的会议时,会触发Item_Change事件2次.

我不知道为什么它会发射两次.

任何人都可以给出原因吗?

解决方案

我没有找到任何完全清晰的解决方案.我实施的最好的想法是创建已经处理过的任务列表,并为它们设置时间有效性.

在事件开始时,我检查是否有任何已处理的任务已过时,并将其从列表中删除.这样,我可以使列表变小并防止内存泄漏.

class TaskItem
{
    private int milisecods = 200;
    DateTime Now
    {
        get
        {
            return DateTime.Now;
        }
    }

    public TaskItem()
    {
        this.Created = Now;
    }

    private DateTime Created;

    public string EntryId { get; set; }

    public bool OutDated
    {
        get
        {
            return this.Created.AddMilliseconds(milisecods) > Now;
        }
    }
}

List<TaskItem> TaskItemsList = new List<TaskItem>();

private void TaskItems_ItemChange(object Item)
{
    this.TaskItemsList.RemoveAll(x => x.OutDated);
    MailItem element = Item as MailItem;
    if (element != null)
    {
        if (element.FlagStatus == OlFlagStatus.olFlagComplete)
        {
            if (this.TaskItemsList.Any(x => x.EntryId == element.EntryID) == false)
            {
                this.TaskItemsList.Add(new TaskItem { EntryId = element.EntryID });
                new WcfClient().ProcesOutlookTask(TaskActionType.Finished);
            }
        }
    }
}

如果您对工作解决方案感兴趣,可以在存储库中找到以上代码.. >

I am developing outlook add-in for which I have to track appointment item add, change & remove events. So I am using bellow code.

   public Microsoft.Office.Interop.Outlook.Application app = null;
   public Outlook.NameSpace ns = null;
   public Outlook.MAPIFolder calendar = null;
   public Outlook.Items appointments = null;

private void ThisAddIn_Startup(object sender, System.EventArgs e) 
{
    app = Application;
    ns = app.GetNamespace("mapi");
    ns.Logon("", "", true, true);
    calendar = ns.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
    appointments = calendar.Items;

    appointments.ItemAdd += Item_Add;
    appointments.ItemChange += Item_Change;
    appointments.ItemRemove += Item_Remove;
}

private void Item_Add(object item) 
{
   // some logic
}

private void Item_Change(object item) 
{
  // some logic
}

private void Item_Remove() 
{
  // some logic
}

Now when I add meeting at that time Item_Add event is called.

When I update that created meeting then Item_Change event is fired 2 times.

I not able to get why it is firing 2 times.

Can any one give possible reason for it ?

解决方案

I haven't found any completely clear solution. The best idea which I implemented was to create list of Tasks which already had been processed and set the time validity for them.

At the beginning of the event I check if any of the processed task is outdated and remove it from the list. This way I keep list small and protect against memory leak.

class TaskItem
{
    private int milisecods = 200;
    DateTime Now
    {
        get
        {
            return DateTime.Now;
        }
    }

    public TaskItem()
    {
        this.Created = Now;
    }

    private DateTime Created;

    public string EntryId { get; set; }

    public bool OutDated
    {
        get
        {
            return this.Created.AddMilliseconds(milisecods) > Now;
        }
    }
}

List<TaskItem> TaskItemsList = new List<TaskItem>();

private void TaskItems_ItemChange(object Item)
{
    this.TaskItemsList.RemoveAll(x => x.OutDated);
    MailItem element = Item as MailItem;
    if (element != null)
    {
        if (element.FlagStatus == OlFlagStatus.olFlagComplete)
        {
            if (this.TaskItemsList.Any(x => x.EntryId == element.EntryID) == false)
            {
                this.TaskItemsList.Add(new TaskItem { EntryId = element.EntryID });
                new WcfClient().ProcesOutlookTask(TaskActionType.Finished);
            }
        }
    }
}

If you are interested in the working solution you can find above code in the repository.

这篇关于在C#中两次调用Outlook的约会项更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆