title: how to cleanup view model properly?
body: <p>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 <code>ManagementEventWatcher</code> and start it. My view model implements <code>IDisposable</code>, so I stop the watcher in the Dispose method.</p>
  
  <p>When I embed the custom control into a window, and then close the window to exit the application it throws an <code>InvalidComObjectException</code> 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.</p>
  
  <p>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.</p>
  
  <p>Any advice?
  Thanks
  Konstantin</p>
  
  <pre><code>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;
          }
      }
  }
  </code></pre>