title: WPF and background worker and the calling thread must be STA
body: <p>I've implemented the solution from Stack Overflow question <em><a href="https://stackoverflow.com/questions/21076203">Implement progressbar in this simple WPF application</a></em>.</p>
  
  <p>MainWindow has its own viewmodel. Inside that viewmodel I receive the user's input and consume the WCF service using a background worker. After WCF serves the data back, I'm trying to display it in a new window. This is where the error occurs:</p>
  
  <blockquote>
    <p>The calling thread must be STA, because many UI components require
    this.</p>
  </blockquote>
  
  <p>I tried to put the <code>[STAThread]</code> attribute on the MainWindow <a href="http://en.wiktionary.org/wiki/code-behind" rel="nofollow noreferrer">code-behind</a> as well as inside the MainWindowViewModel contructor. In both cases nothing changed.</p>
  
  <p>What am I missing?</p>
  
  <p><strong>Update</strong>
  After user clicks command in viewmodel call LoadData method</p>
  
  <pre><code>private void LoadData(string searchBy)
  {
      IsBusy = true;
      BackgroundWorker worker = new BackgroundWorker();
      worker.DoWork += (o, ea) =&gt;
      {
          switch (searchBy)
          {
              // WCF call to load data
          }
      }
  
      worker.RunWorkerCompleted += (o, ea) =&gt;
      {
         IsBusy = false;
      };
  
      worker.RunWorkerAsync();
  </code></pre>