ItemChangeEvent多次触发 [英] ItemChangeEvent fired multiple times

查看:244
本文介绍了ItemChangeEvent多次触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我致力于连接到Owncloud(Caldav,Cardav)的Outlook加载项。
我使用事件处理程序来检测用户何时删除,创建或更新联系人项目。
在这种情况下,它将通知服务器并在他这一边进行更新。

I work on Outlook addin that is connected to an Owncloud (Caldav, Cardav). I use eventhandlers to detect when a user delete, create or update a contact item. In this case this will notify the server and do an update on his side.

storage.Aitems[storage.Aitems.Count - 1].ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(contact_item_add);
storage.Aitems[storage.Aitems.Count - 1].ItemChange += new Outlook.ItemsEvents_ItemChangeEventHandler(contact_item_change);
((MAPIFolderEvents_12_Event)storage.Afolders[storage.Afolders.Count - 1]).BeforeItemMove += new MAPIFolderEvents_12_BeforeItemMoveEventHandler(contact_item_before_move);

当我从服务器上找到新的联系人项目时,我将其添加到通讯录文件夹中。 Outlook检测到新项目或更新,然后触发事件处理程序。如果只有一个呼叫(我可以说来自服务器的此消息不再通知服务器,或者来自用户的通知服务器),则很好,但是我不能这样做,因为事件被多次触发。

When i find new contacts items from the server i add them right the addressbook folder. Outlook detect a new item or a update and then fire the eventshandlers. This is good in case there is only one call (I can say this from the server dont notify again the server or this is from the user notify the server) but i can't because the events are fired multiple times.

static void contact_item_change(object Item) {
        Microsoft.Office.Interop.Outlook.ContactItem contact = (Microsoft.Office.Interop.Outlook.ContactItem)Item;
        System.Diagnostics.Debug.WriteLine("[Modification contact]" + contact.FullName);
        // Need to know if item was created by code (server) or user
        Main.SyncContact(contact);
 }

是否可以知道是否通过GUI或代码?
由于多次调用事件,我无法设置变量来知道它是由用户还是由我的代码创建的。

Is it possible to know if an item is created trough the GUI or in my code ? I can't set a variable to know if it was created by user or by my code because of the multiple calls of events.

BTW我已经成功了触发一次添加和删除操作:

BTW i already succeed to fire add and delete only one time :

static void contact_item_add(object Item) {
        Microsoft.Office.Interop.Outlook.ContactItem contact = (Microsoft.Office.Interop.Outlook.ContactItem)Item;
        if (Contact.GetCreatedByUserProperty(contact, "USER")) {
            if (storage.PreviousAddedContactId != contact.EntryID)
            {
                System.Diagnostics.Debug.WriteLine("[Ajout contact]" + contact.FullName);
                Main.SyncContact(contact);
                storage.PreviousAddedContactId = contact.EntryID;
            }
        }
    }


static void contact_item_before_move(object item, MAPIFolder destinationFolder, ref bool cancel) {
        if ((destinationFolder == null) || (IsDeletedItemsFolder(destinationFolder))) {
            ContactItem contact = (item as ContactItem);
            if (storage.PreviousDeletedContactId != contact.EntryID)
            {
                System.Diagnostics.Debug.WriteLine("[Suppression du contact]" + contact.FullName);
                Main.DeleteContact(contact);
                storage.PreviousDeletedContactId = contact.EntryID;
            }
        }
    }

谢谢!

推荐答案

您可以在调用MailItem.Save之后立即读取MailItem.LastModificationTime。当发生ItemChange事件时,可以将新的修改时间与缓存的时间进行比较,并检查它是否大于某个增量(1秒?)。

You can read MailItem.LastModificationTime immediately after calling MailItem.Save. When ItemChange event fires, you can compare the new modification time with what you cached and check if it is greater than some delta (1 second?).

如果是Exchange在商店中,您还可以检索PR_CHANGE_KEY(DASL名称 http://schemas.microsoft.com/mapi/proptag/0x65E20102 )属性-每次修改后都会更改。

In case of Exchange store, you can also retrieve PR_CHANGE_KEY (DASL name http://schemas.microsoft.com/mapi/proptag/0x65E20102) property - it will change after each modification.

这篇关于ItemChangeEvent多次触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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