C#Outlook2007COM互操作应用程序不退出!为什么以下代码不退出通过COM互操作创建的Outlook2007进程的任何想法?Microsoft.Office.Interop.Outlook.Applicationapp=newMicrosoft.Office.Interop.Outlook.Application();varitem=app.Session.OpenSharedItem("C:\test.msg")asMicrosoft.Office.Interop.Outlook.MailItem;stringbody=item.HTMLBody;intatt=item.Attachments.Count;(项目为Microsoft.Office.Interop.Outlook._MailItem)。关闭(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard);系统。Runtime.InteropServices.Marshal.ReleaseComObject(项目);(作为Microsoft.Office.Interop.Outlook._Application的应用程序).??Quit();System.Runtime.InteropServices.Marshal.ReleaseComObject(app);System.Diagnostics.Debugger.Break();使用Word的一个几乎相同的片段有效,所以我想知道我是否忘记清理某些东西......您的代码中引用了第三个COM对象:app.Session。这也必须正确发布。试试这个代码:Microsoft.Office.Interop.Outlook.Applicationapp=null;Microsoft.Office.Interop.Outlook.NameSpace会话=null;Microsoft.Office.Interop.Outlook.MailItem项目=null;尝试{app=newMicrosoft.Office.Interop.Outlook.Application();session=app.Session;item=session.OpenSharedItem("C:\test.msg")asMicrosoft.Office.Interop.Outlook.MailItem;stringbody=item.HTMLBody;intatt=item.Attachments.Count;(项目为Microsoft.Office.Interop.Outlook._MailItem)。关闭(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard);(作为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);我不知道OfficeCOMInterops的细节,但这里有一些MSDN文章中建议的代码。它表明双重收集/等待和清除指针有助于RCW包装器清理。项目=空;应用程序退出();应用程序=空;GC.收集();GC.WaitForPendingFinalizers();GC.收集();GC.WaitForPendingFinalizers();但是,该url还表明while(Marshal.ReleaseComObject(app)>0){}如果您能提供帮助,我个人强烈反对,因为您基本上只是在破坏您的AppDomain的RCW(正如文章所指出的)。[编辑:.Net垃圾收集器在调试器和发布代码中的行为也非常不同,因此在调试器外部进行测试很重要]试试这个,它对我有用,有几秒钟的时间Delay:app.Quit();//System.Runtime.InteropServices.Marshal.ReleaseComObject(app);GC.收集();GC.WaitForPendingFinalizers();尝试在app.Quit();之后集中注意力以上就是C#学习教程:C#Outlook2007COM互操作应用程序不退出!分享的所有内容,如果对你有用,需要进一步了解C#学习教程,希望大家多多关注—//ReleaseComObject(xApp);GC.WaitForPendingFinalizers();GC.收集();本文来自网络收藏,不代表立场,如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处:
