问题挂互操作COM对象 [英] Problem with hanging interop COM objects

查看:104
本文介绍了问题挂互操作COM对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用COM互操作,以创建一个在Excel中打开客户机上的电子表格的应用程序。它的出现,然而,当Excel是由用户,如果我看任务管理器关闭EXCEL.EXE过程并不总是结束。

I have an application that uses COM interop to create a spreadsheet which is opened in Excel on the client's machine. It appears, however, that the EXCEL.exe process isn't always ended when Excel is closed by the user if I look at Task Manager.

如果我被保存工作簿和编程关闭Excel中,我只想用对Marshal.ReleaseComObject()来清理,但因为我这取决于程序的手动接近我,我不是知道该怎么做。有什么建议?

If I was saving the workbook and programmatically closing Excel, I would just use Marshal.ReleaseComObject() to clean up, but since I'm depending on a manual close of the program, I'm not sure what to do. Any suggestions?

推荐答案

Excel不能终止,直到所有的进程外对象被释放。因此,它只是隐藏它的用户界面,并保持运行。直到你的程序退出,或者你空所有您的Excel对象引用和终结线程运行。退出你的程序将是一个显而易见的解决方案。或者,你可以强制终结器线程与运行:

Excel cannot terminate until all its out-of-process objects are released. So it just hides its user interface and keeps running. Until either your program quits or you null all your Excel object references and the finalizer thread runs. Quitting your program would be an obvious solution. Or you can force the finalizer thread to run with:

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



ReleaseComObject的()很少奏效,因为它是那么容易忘记的中介COM对象引用。像簿[指数],这是你看不到一个枚举。让GC作出这样的决定本身将是更明智的选择,如果继续运行,做工作,那么会发生。

ReleaseComObject() rarely works because it is so easy to forget an intermediary COM object reference. Like WorkBooks[index], that's an enumerator you don't see. Letting the GC make that decision for itself would be the wiser choice, if you keep running and doing work then that will happen.

这篇关于问题挂互操作COM对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆