I’m using a ManagementEventWatcher in my C# library which is imported (referenced) into another project.
The problem I have is that in my case the ManagementEventWatcher has to be “stopped” and “disposed” before my application is closed.
I’m only allowed to modify the library and not the applications which will implement the library.
I’ve tried the following:
public class MyClass:IDisposable ...
And then
public void Dispose()
{
_wmiWatcherRemoved.Stop();
_wmiWatcherRemoved.Dispose();
}
But Dispose will never be executed.
So the applications get the following exception:
COM object that has been separated from its underlying RCW can not be used
Any ideas?
Thank you for your efforts.
I’m assuming from the wording of your question that applications using your library encounter the exception you describe as they are closing down?
If this is the case, you might try registering a handler for the AppDomain ProcessExit event, in which you would cause
Stop()andDispose()to be called on the watcher instance.