title: C#, MVVM, Tasks and the UI Thread
body: <p>We have an application built according to the MVVM pattern.  At various times we kick off tasks to go to a database to retrieve data, we then populate an ObservableCollection to which a WPF control is bound with that data.  </p>
  
  <p>We are a little confused, when we populate the ObservableCollection we are doing so on the task thread, not the UI thread, yet the UI is still updated/behaving correctly.  We were expecting an error and to have to change the code to populate the collection on the UI thread.</p>
  
  <p>Is this a dangerous scenario and should we populate on the UI thread anyway?</p>
  
  <p>Code to get data:</p>
  
  <pre><code>Task.Factory.StartNew(() =&gt;
      UiDataProvider.RefreshForwardContractReport(fcrIdField.Value)
  )
  .ContinueWith(task =&gt; {
      if (task.Result.OperationSuccess)
      {
          // This updates the ObseravableCollection, should it be run on UI thread??
          RefreshReport(task.Result.OperationResult);
      }
  });
  </code></pre>