我在C#中创建了一个VSTO,它应该挂钩Outlook 2007的NewMailEx事件。但是,当我进行手动发送/接收,或者收件箱中只有1封未读邮件时,它有时不会触发。在消息实际到达之前,它几乎就好像在收件箱中闪现一样。

除了使用VSTO的ItemAdd或NewMailEX之外,还有更好的方法来监控每次的新消息吗?

有帮助吗?

解决方案

原因是:“GC收集.NET对象,其中包含来自Outlook的COM对象”"。 解决方案是保持对此.NET对象的引用。最简单的方法是:

// this is helper collection.
// there are all wrapper objects
// , which should not be collected by GC
private List<object> holdedObjects = new List<object>();

// hooks necesary events
void HookEvents() {
    // finds button in commandbars
    CommandBarButton btnSomeButton = FindCommandBarButton( "MyButton ");
    // hooks "Click" event
    btnSomeButton.Click += btnSomeButton_Click;
    // add "btnSomeButton" object to collection and
    // and prevent themfrom collecting by GC
    holdedObjects.Add( btnSomeButton );
}

如果需要,您还可以为此(和其他)具体按钮(或其他对象)设置一个特殊字段。但这是最常见的解决方案。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top