COM对象仍然有效 [英] COM Objects Still Alive

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

问题描述

我使用 Microsoft.Office.Interop.Excel和几个变量,这些变量在我的程序运行时会重新分配给不同的基础COM对象.每当尝试重新分配变量时,我都尝试使用下面的代码释放所有对象,但是当程序退出时,Excel.exe进程不会关闭.有没有办法找到由程序/进程启动的所有COM对象?这样,我可以遍历它们,将它们传递给下面定义的releaseCOMObject方法.或者如何获取ExcelApplication进程的ID,以便在程序存在时可以将其关闭?
请提供帮助.

private void releaseCOMObject(对象COMObject){
try {
if(COMObject!= null)
while(System.Runtime.InteropServices.Marshal.ReleaseComObject(COMObject )> 0);
}
最终{
} COMObject = null;
}
}

推荐答案



以下链接显示了如何获取进程数组:
http://msdn.microsoft.com/zh-cn/library/system.diagnostics.process.getprocesses(VS.71).aspx

Process类还具有ID属性.

我在一个项目中也遇到了类似的问题.有很多所谓的解决方案".当我搜索网络时,却没有一个对我有用.
因此,我的解决方法是从应用程序启动Excel进程ID之后,我用它杀死了Excel进程.存储PID.这对我有用.我知道这可能不是最好的解决方案,但对我来说效果很好.

BR,
Tomi
Hi,

The following link shows how to get array of processes:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.getprocesses(VS.71).aspx

Process class also has ID property.

I also ran into similar problem in one project. There was a lot of so called "solutions" when I searched the net but none of them worked for me.
So my workaround for this was that I took the Excels process ID after I started it from my app and when I was done with it, I killed the Excel process with that stored PID. This worked for me. I know that is might not be the best solution but it worked fine in my case.

BR,
Tomi


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

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