WMI异常:"已经从它的基础RCW分开COM对象,不能使用与QUOT; [英] WMI Exception: "COM object that has been separated from its underlying RCW cannot be used"

查看:302
本文介绍了WMI异常:"已经从它的基础RCW分开COM对象,不能使用与QUOT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要订阅一个WMI事件,并收到不能用于已从其基础RCW分开COM对象的错误我的应用程序关闭时。这个问题已经被问之前,但它是从我的情况完全不同。

I'm subscribing to a WMI event and receiving a "COM object that has been separated from its underlying RCW cannot be used" error when my application closes. This question has been asked before, but it is quite different from my circumstances.

我打电话是从我的主线程代码:

I am calling this code from my main thread:

string strComputer = @".";
ManagementScope scope = new ManagementScope(@"\\" + strComputer + @"\root\wmi");
scope.Connect();

EventQuery query = new EventQuery("Select * from MSNdis_StatusMediaDisconnect");

ManagementEventWatcher watcher = new ManagementEventWatcher(scope, query);

watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived); // some function that does stuff when the event occurs.
watcher.Start();



该事件正确报告。我怀疑这个问题是涉及到我的应用程序关闭时,这些对象被释放的方式。如何防止错误?我的应用程序关闭之前,我应该明确处置的守望者,范围和查询?

The event is reported correctly. I suspect the problem is related to the way these objects are deallocated when my application closes. How do I prevent the error? Should I explicitly Dispose of the watcher, scope and query before my application closes?

推荐答案

好吧,WMI是启用COM,异常不完全是神秘的。我怀疑在终结比赛,尝试通过调用观察者的停止()你让前程序终止方法修复它。

Well, WMI is COM enabled, the exception is not entirely mysterious. I suspect a race in the finalizer, try fixing it by calling the watcher's Stop() method before you let your program terminate.

这篇关于WMI异常:"已经从它的基础RCW分开COM对象,不能使用与QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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