title: WPF failing to update state when multi-threading
body: <p>Running an issue with multi-threading and WPF. I don't really know what I'm doing and the usual stackoverflow answers aren't working out.</p>
  <p>First off, a bunch of WPF windows are created via:</p>
  <pre class="lang-cs prettyprint-override"><code>var thread = new Thread(() =&gt;
  {
    var bar = new MainWindow(command.Monitor, _workspaceService, _bus);
    bar.Show();
    System.Windows.Threading.Dispatcher.Run();
  });
  
  thread.Name = &quot;Bar&quot;;
  thread.SetApartmentState(ApartmentState.STA);
  thread.Start();
  </code></pre>
  <p>In the ctor of the spawned windows, a viewmodel is created and an event is listened to where the viewmodel should be changed.</p>
  <pre class="lang-cs prettyprint-override"><code>this.DataContext = new BarViewModel();
  
  // Listen to an event propagated on main thread.
  _bus.Events.Where(@event =&gt; @event is WorkspaceAttachedEvent).Subscribe(observer =&gt; 
  {
    // Refresh contents of viewmodel.
    (this.DataContext as BarViewModel).SetWorkspaces(monitor.Children);
  });
  </code></pre>
  <p>The viewmodel state is modified like this:</p>
  <pre class="lang-cs prettyprint-override"><code>public void SetWorkspaces(IEnumerable&lt;Workspace&gt; workspaces)
  {
    Application.Current.Dispatcher.Invoke((Action)delegate
   {
     this.Workspaces.Clear(); // this.Workspaces is an `ObservableCollection&lt;Workspace&gt;`
  
     foreach (var workspace in workspaces)
       this.Workspaces.Add(workspace);
  
     this.OnPropertyChanged(&quot;Workspaces&quot;);
   });
  }
  </code></pre>
  <p>Problem is accessing <code>Application.Current.Dispatcher</code> results in a <code>NullReferenceException</code>. Is there something wrong with the way the windows are spawned?</p>