16

I have a view model that is used as the data source for my custom control. In the view model's constructor I set up a WMI ManagementEventWatcher and start it. My view model implements IDisposable, so I stop the watcher in the Dispose method.

When I embed the custom control into a window, and then close the window to exit the application it throws an InvalidComObjectException saying "COM object that has been separated from its underlying RCW cannot be used". This happens because of my watcher, and if I do not create it, there is no exception. there is no additional information about the exception such as stack trace, etc.

My guess is that something keeps the view model until the thread that the watcher uses terminates but before the watcher is stopped, and I do not know how to handle this.

Any advice? Thanks Konstantin

public abstract class ViewModelBase : IDisposable, ...
{
    ...

    protected virtual void OnDispose() { }

    void IDisposable.Dispose()
    {
        this.OnDispose();
    }
}

public class DirectorySelector : ViewModelBase
{
    private ManagementEventWatcher watcher;

    private void OnWMIEvent(object sender, EventArrivedEventArgs e)
    {
        ...
    }

    protected override void OnDispose()
    {
        if (this.watcher != null)
        {
            this.watcher.Stop();
            this.watcher = null;
        }
        base.OnDispose();
    }

    public DirectorySelector()
    {
        try
        {
            this.watcher = new ManagementEventWatcher(new WqlEventQuery(...));

            this.watcher.EventArrived += new EventArrivedEventHandler(this.OnWMIEvent);
            this.watcher.Start();
        }
        catch (ManagementException)
        {
            this.watcher = null;
        }
    }
}
Ramzi Khahil
  • 4,932
  • 4
  • 35
  • 69
akonsu
  • 28,824
  • 33
  • 119
  • 194
  • would be nice to know what language you are workin with ;) but anyway... a "view model"? sound like you are kinda mixing things which should be really separeted... – Andreas Linden Sep 23 '10 at 18:08
  • i am using C#. i am working on a directory selector control for WPF, and this control must be able to handle drive mounts and unmounts, and cd-rom insertions/removals. since MVVM is the preferred way to separate UI from the code, i am using a view model that returns the list of drives, directories, etc, and monitors the drives. – akonsu Sep 23 '10 at 18:12

1 Answers1

6

this article has the solution: Disposing WPF User Controls

basically, WPF dos not seem to use IDisposable anywhere, so the app needs to cleanup itself explicitly. so in my case, i subscribe to the Dispatcher.ShutdownStarted event from my control that uses the view model that needs to be disposed, and dispose the control's DataContext from the event handler.

Community
  • 1
  • 1
akonsu
  • 28,824
  • 33
  • 119
  • 194