title: Update DataContext from background thread
body: <p>I fetch data for a wpf window in a backgroundthread like this [framework 4.0 with async/await]:</p>
  
  <pre><code>async void refresh()
  {
      // returns object of type Instances
      DataContext = await Task.Factory.StartNew(() =&gt; serviceagent.GetInstances());
      var instances = DataContext as Instances;
      await Task.Factory.StartNew(() =&gt; serviceagent.GetGroups(instances));
      // * problem here * instances.Groups is filled but UI not updated
  }
  </code></pre>
  
  <p>When I include the actions of GetGroups in GetInstances the UI shows the groups.<br>
  When I update in a seperate action the DataContext includes the groups correclty but the UI doesn't show them.  </p>
  
  <p>In the <code>GetGroups()</code> method I inlcuded <code>NotifyCollectionChangedAction.Reset</code> for the <code>ObservableCollection</code> of groups and this doesn't help.<br>
  Extra strange is that I call <code>NotifyCollectionChangedAction.Reset</code> on the list only once, but is executed three times, while the list has ten items?!</p>
  
  <p>I can solve the issue by writing:</p>
  
  <pre><code>DataContext = await Task.Factory.StartNew(() =&gt; serviceagent.GetGroups(instances));
  </code></pre>
  
  <p>But is this the regular way for updating DataContxt and UI via a backgound process?<br>
  Actually I only want to update the existing DataContext without setting it again?</p>
  
  <p>EDIT: <code>serviceagent.GetGroups(instances)</code> in more detail:</p>
  
  <pre><code>public void GetGroups(Instances instances)
  {
      // web call
      instances.Admin = service.GetAdmin();
  
      // set groups for binding in UI
      instances.Groups = new ViewModelCollection&lt;Groep&gt;(instances.Admin.Groups);
  
      // this code has no effect
      instances.Groups.RaiseCollectionChanged();
  }
  </code></pre>
  
  <p>Here <code>ViewModelCollection&lt;T&gt;</code> inherits from <code>ObservableCollection&lt;T&gt;</code> and I added the method:</p>
  
  <pre><code>public void RaiseCollectionChanged()
  {
      var handler = CollectionChanged;
      if (handler != null)
      {
          Trace.WriteLine("collection changed");
          var e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
          handler(this, e);
      }
  }
  </code></pre>