WMI 异常:“无法使用已与其基础 RCW 分离的 COM 对象”
我正在订阅 WMI 事件,并在应用程序关闭时收到“无法使用已与其底层 RCW 分离的 COM 对象”错误。这个问题之前已经被问过,但是这与我的情况完全不同。
我从主线程调用此代码:
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();
事件已正确报告。我怀疑问题与应用程序关闭时这些对象的释放方式有关。我该如何防止该错误?我应该在应用程序关闭之前显式处置观察程序、作用域和查询吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,WMI 启用了 COM,该异常并不完全神秘。我怀疑终结器中存在竞争,请在让程序终止之前尝试通过调用观察者的 Stop() 方法来修复它。
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.