知道为什么以下代码不退出通过 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 可以得到几乎相同的片段,所以我想知道我是否忘记清理一些东西......
您的代码中引用了第三个 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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)