7

为什么以下代码不退出通过 COM 互操作创建的 Outlook 2007 进程的任何想法?

Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();

var item = app.Session.OpenSharedItem("C:\\test.msg") as Microsoft.Office.Interop.Outlook.MailItem;
string body = item.HTMLBody;
int att = item.Attachments.Count;

(item as Microsoft.Office.Interop.Outlook._MailItem).Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard);
System.Runtime.InteropServices.Marshal.ReleaseComObject(item);

(app as Microsoft.Office.Interop.Outlook._Application).Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
System.Diagnostics.Debugger.Break();

使用 Word 的几乎相同的片段可以工作,所以我想知道我是否忘记清理某些东西......

4

4 回答 4

10

您的代码中引用了第三个 COM 对象:app.Session. 这也必须正确释放。试试这个代码:

Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook.NameSpace session = null;
Microsoft.Office.Interop.Outlook.MailItem item = null;

try {
    app = new Microsoft.Office.Interop.Outlook.Application();
    session = app.Session;
    item = session.OpenSharedItem("C:\\test.msg") as Microsoft.Office.Interop.Outlook.MailItem;

    string body = item.HTMLBody;
    int att = item.Attachments.Count;

    (item as Microsoft.Office.Interop.Outlook._MailItem).Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard);

    (app as Microsoft.Office.Interop.Outlook._Application).Quit();
} finally {
    if(item != null) {
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(item);
    }
    if(session != null) {
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(session);
    }
    if(app != null) {
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(app);
    }
}
于 2009-12-15T14:03:55.703 回答
2

我不知道 Office COM Interops 的细节,但这里有一些MSDN 文章中建议的代码。它表明指针的双重收集/等待和清除有助于 RCW 包装器清理。

item = null;
app.Quit();
app = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();

然而,该网址也表明

while (Marshal.ReleaseComObject(app) > 0) { }

如果您可以提供帮助,我个人强烈建议您不要这样做,因为您基本上刚刚为您的 AppDomain 销毁了 RCW(正如文章指出的那样)。

[编辑:.Net 垃圾收集器在调试器与发布代码内部的行为也非常不同,因此在调试器外部测试这一点非常重要]

于 2009-12-15T14:16:29.813 回答
0

在 app.Quit(); 之后尝试以下操作

// ReleaseComObject(xApp);
GC.WaitForPendingFinalizers();
GC.Collect();
于 2009-12-15T13:25:52.383 回答
0

试试这个,它对我有用,在它开始之前会有几秒钟的延迟:

app.Quit(); //
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
GC.Collect();
GC.WaitForPendingFinalizers();
于 2009-12-15T13:55:32.557 回答