title: BackgroundWorker RunWorkerCompleted not executed on UI thread
body: <p>I have written a small wrapper around BackgroundWorker:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class Worker
  {
    public void Execute&lt;T&gt;(Func&lt;T&gt; work, Action&lt;Exception, T&gt; workCompleted)
    {
      var worker = new BackgroundWorker();
      worker.DoWork += (o, ea) =&gt; { ea.Result = work(); };
      worker.RunWorkerCompleted += (o, ea) =&gt; { workCompleted(ea.Error, (T)ea.Result); };
      worker.RunWorkerAsync();
    }
  }
  </code></pre>
  
  <p>I use it in the viewmodel (which is created in XAML) like so:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class MainWindowViewmodel : INotifyPropertyChanged
  {
    private ObservableCollection&lt;Job&gt; _jobs = new ObservableCollection&lt;Job&gt;();
    public ICollectionView JobsView { get; } = CollectionViewSource.GetDefaultView(_jobs);
    public RelayCommand RefreshJobsCommand =&gt; new RelayCommand(x =&gt; UpdateJobs()); /*edit1*/
  
    private void UpdateJobs() /*executed via command binding*/
    {
      new Worker().Execute(() =&gt; _webServiceManager.JobService.GetJobsByDate(settings.Limit, DateTime.Now.AddDays(-365)), (ex, jobArray) =&gt;
        {
          jobArray.toList().ForEach(j=&gt;_jobs.Add(j));
        });
    }
  }
  </code></pre>
  
  <p>When jobs are added in <code>workCompleted</code> to ObservableCollection <code>_jobs</code>, I get the following exception:
  <code>This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.</code></p>
  
  <p>So obviously the <code>RunWorkerCompleted</code> callback doesn't run on the UI thread. But as far as I know it should run on the one thread from which <code>RunWorkerAsync</code> was called? And this should be the UI thread, since it is called from inside the viewmodel (which was created from XAML) from a command binding (also UI thread), right?</p>
  
  <p>What am I missing here?</p>
  
  <p><strong>Edit2:</strong>
  I could narrow down the problem to the initial loading of jobs. This is done from the ctor of <code>MainWindow</code> and will result in the described problem. Any additional updates afterwards using the UI button via command binding work absolutely fine.</p>
  
  <p>So the problem is, on initial loading, <code>MainWindow</code> ctor, <code>ViewModel</code> ctor and <code>Worker.Execute()</code> are executed on the UI thread, but <code>workCompleted</code> is not, although the <code>SynchronizationContext</code> should be set to the UI thread at the moment of invoking <code>RunWorkerAsync</code>.</p>
  
  <p>Whenever the next loading is triggered via command binding, the <code>workCompleted</code> is also set to the <code>SynchronizationContext</code> of the UI thread.</p>
  
  <p>Any idea why this isn't working in the first place?</p>
  
  <pre><code>public partial class MainWindow : Window
  {
     private MainWindowViewmodel _viewmodel;
  
     public MainWindow(Connection connection)
     {
        InitializeComponent();
        Debug.WriteLine("mainwindow-ctor: " + Thread.CurrentThread.ManagedThreadId.ToString());
        _viewmodel = (MainWindowViewmodel)DataContext;
        _viewmodel.UpdateJobs();
     }
  }
  </code></pre>