WMI Exception: “COM object that has been separated from its underlying RCW cannot be used”

允我心安 提交于 2019-12-23 16:08:31

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/3953864/wmi-exception-com-object-that-has-been-separated-from-its-underlying-rcw-canno

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!