c# - 如何在 Visual Studio Tools For Office (VSTO) 2010 Outlook 加载项完成后进行清理

标签 c# outlook garbage-collection vsto

Microsoft 有一个简单的 VSTO 2010 Outlook 加载项演练,它完美地说明了我在我支持的更复杂的加载项中看到的问题。这是演练的链接:

FirstOutlookAddin Walkthrough

下面是我复制到 C# VSTO 2010 Outlook 加载项项目中的演练代码:

using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;

namespace FirstOutlookAddIn
{
    public partial class ThisAddIn
    {
        private Outlook.Inspectors inspectors;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            inspectors = this.Application.Inspectors;
        inspectors.NewInspector +=
            new Microsoft.Office.Interop.Outlook
                   .InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
    }

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

    void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
    {
        Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;
        if (mailItem != null)
        {
            if (mailItem.EntryID == null)
            {
                mailItem.Subject = "Added Text";
                mailItem.Body = "Added Text to Body";
            }
        }
    }

    #region VSTO generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InternalStartup()
    {
        this.Startup += new System.EventHandler(ThisAddIn_Startup);
        this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
    }      
    #endregion
}
}

我遇到的问题是将此代码与 Entrust 结合使用以发送加密电子邮件,然后撤回电子邮件并在编辑后重新发送。一旦我重新发送它(在召回和编辑之后),它就会破坏消息,当我尝试打开它时,我收到以下错误:

Sorry, we're having trouble opening this item. this could be temporary, but if you see it again you might want to restart Outlook. An error occurred in the underlying security system. An internal error occurred.

我几乎可以肯定,问题在于本地使用的一个或多个对象没有被垃圾收集器自动清理,但我不确定如何强制垃圾收集器 (GC ) 进行清理,使其正常工作。我一直在尝试将本地对象设置为 null,并找到了一些讨论调用的帖子:

GC.Collect();
GC.WaitForPendingFinalizers();

也一直在尝试,但到目前为止运气不佳。任何人都可以提供一些关于如何解决此问题的指示吗?

最佳答案

如果你想从堆中清除未使用的 COM 对象,你需要调用 GC 两次。例如:

 GC.Collect();
 GC.WaitForPendingFinalizers();
 GC.Collect();
 GC.WaitForPendingFinalizers();

但更好的方法是使用System.Runtime.InteropServices.Marshal.ReleaseComObject使用完毕后释放 Outlook 对象。如果您的加载项试图枚举存储在 Microsoft Exchange Server 上的集合中超过 256 个 Outlook 项目,这一点尤其重要。然后在 Visual Basic 中将变量设置为 Nothing(在 C# 中为 null)以释放对该对象的引用。您可以在 Systematically Releasing Objects 中阅读更多相关信息MSDN 中的文章。

关于c# - 如何在 Visual Studio Tools For Office (VSTO) 2010 Outlook 加载项完成后进行清理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31544379/

相关文章:

c# - 在查看模式设置为滚动时调整 FlowDocumentReader 滚动增量?

c# - 如何将属性的 .NET 反射与每个对象相关联?

c# - 如何在 C# 中递归遍历并记录 HttpRequestMessage 的属性?

c# - 检查对象的实例是否是只读的

c# - ICAL.NET 重复元素 : Fifth or Last Sunday of every Month

vba - 发送电子邮件时运行宏

apache-flex - 显示Flex对象引用

java - 为什么CMS Final Remark中ParNew,sys time这么长?

.net - 如何防止控件更新线程超时?

html - outlook html 时事通讯中的空白