如何正确清理Excel互操作对象?

浪尽此生 提交于 2020-04-28 02:25:41

问题:

I'm using the Excel interop in C# ( ApplicationClass ) and have placed the following code in my finally clause: 我在C#( ApplicationClass )中使用Excel互操作,并将以下代码放在我的finally子句中:

while (System.Runtime.InteropServices.Marshal.ReleaseComObject(excelSheet) != 0) { }
excelSheet = null;
GC.Collect();
GC.WaitForPendingFinalizers();

Although this kind of works, the Excel.exe process is still in the background even after I close Excel. 尽管这种工作有效,但是即使我关闭Excel, Excel.exe进程仍在后台。 It is only released once my application is manually closed. 仅在我的应用程序手动关闭后才释放它。

What am I doing wrong, or is there an alternative to ensure interop objects are properly disposed of? 我在做什么错,还是有其他方法可以确保互操作对象得到正确处理?


解决方案:

参考一: https://stackoom.com/question/fHm/如何正确清理Excel互操作对象
参考二: https://oldbug.net/q/fHm/How-do-I-properly-clean-up-Excel-interop-objects
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!