为什么AppointmentItem的发送事件会被多次调用?

时间:2012-05-16 03:08:19

标签: outlook vsto

我为每个AppointmentItem的发送事件添加了一个事件处理程序。这个事件处理程序只是做一些日志记录。我通过Outlook 2003创建会议,然后两次更新会议。最后我查了一下日志。

this.Application.Inspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(_inspectors_NewInspector);

private void _inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector inspector)
{
    if(inspector.CurrentItem is Outlook.AppointmentItem)
    {
        _appointmentEvent = inspector.CurrentItem as Outlook.ItemEvents_10_Event;
        _appointmentEvent.Send += new Microsoft.Office.Interop.Outlook.ItemEvents_10_SendEventHandler(_appointmentEvent_Send);
    }
}

private void _appointmentEvent_Send(ref bool Cancel)
{
    Log.WriteLog("InspectorWrapper: _appointmentEvent_Send Enter");
    Log.WriteLog("InspectorWrapper: _appointmentEvent_Send Exit");
}

我检查日志。我发现send事件处理程序将被多次调用。

  

2012-05-16 10:07:21:066:InspectorWrapper:_appointmentEvent_Send输入
      2012-05-16 10:07:21:067:InspectorWrapper:_appointmentEvent_Send退出
      ...
      2012-05-16 10:07:27:281:InspectorWrapper:_appointmentEvent_Send输入
      2012-05-16 10:07:27:283:InspectorWrapper:_appointmentEvent_Send退出
      2012-05-16 10:07:27:283:InspectorWrapper:_appointmentEvent_Send输入
      2012-05-16 10:07:27:284:InspectorWrapper:_appointmentEvent_Send退出
      ...
      2012-05-16 10:07:32:607:InspectorWrapper:_appointmentEvent_Send输入
      2012-05-16 10:07:32:608:InspectorWrapper:_appointmentEvent_Send退出
      2012-05-16 10:07:32:609:InspectorWrapper:_appointmentEvent_Send输入
      2012-05-16 10:07:32:609:InspectorWrapper:_appointmentEvent_Send退出
      2012-05-16 10:07:32:610:InspectorWrapper:_appointmentEvent_Send输入
      2012-05-16 10:07:32:610:InspectorWrapper:_appointmentEvent_Send退出

为什么?

1 个答案:

答案 0 :(得分:0)

您应该向NewInspector事件添加日志记录,以查看您锁定AppointmentItem.Send事件的次数。我的假设是NewInspector是你的问题。最佳做法是使用自定义InspectorWrapper作为容器来管理活动检查器列表,以避免多个事件锁定。

如需参考,请查看this MSDN reference中的FindOutlookInspector

internal static List<OutlookInspector> m_Windows;       
internal static OutlookInspector FindOutlookInspector(object window)
{
    foreach (OutlookInspector inspector in m_Windows)
        if (inspector.Window == window)
            return inspector;
    return null;
}