title: BackgroundWorker.ProgressChanged event not on Dispatcher Thread
body: <p>I have the following code which updates my progress bar and status bar from a backgroundworker.  I run the same backgroundworker twice.  The first time I run it I call it from the MainWindow constructor it works fine.  At the end of the constructor I setup a timer to call the method every so often.</p>
  
  <pre><code>   System.Threading.TimerCallback timerCallback = new System.Threading.TimerCallback(RefreshWebDataTimer);
      timer = new System.Threading.Timer(
          timerCallback, null,
          Dictionary.MS_TIMER_FIRSTREFRESH_PERIOD,
          Dictionary.MS_TIMER_REFRESH_PERIOD);
  </code></pre>
  
  <p>When calling it from the timer I get the following error:</p>
  
  <blockquote>
    <p>A first chance exception of type 'System.InvalidOperationException'
    occurred in WindowsBase.dll Additional information: The calling thread
    cannot access this object because a different thread owns it.</p>
  </blockquote>
  
  <p>I added some debug and indeed the Dispatcher Thread is on a different thread from the timer and the same thread from the original run.</p>
  
  <pre><code>   private void backgroundWorker_ProgressChanged(object sender,
              ProgressChangedEventArgs e)
          {
              System.Diagnostics.Debug.Print("Current Thread: {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
              System.Diagnostics.Debug.Print("Dispatcher Thread: {0}", progressBar.Dispatcher.Thread.ManagedThreadId);
  
  
              this.progressBar.Visibility = Visibility.Visible;
              this.progressBar.Value = e.ProgressPercentage;
              if (e.UserState != null)
              {
                  this.statusBar.Text = e.UserState.ToString();
              }
          }
  </code></pre>
  
  <blockquote>
    <p>Current Thread: 22 
    Dispatcher Thread: 7</p>
  </blockquote>
  
  <p>I was under the impression that the <code>ProgressChanged</code> and <code>RunWorkerCompleted</code> events always ran on the main UI thread in order to solve this problem and be able to do UI updates.  Apparently, I misunderstand what is going on here.</p>
  
  <p>I updated my solution to use the Dispatcher as follows:</p>
  
  <pre><code>private void backgroundWorker_ProgressChanged(object sender,
      ProgressChangedEventArgs e)
  {
      progressBar.Dispatcher.BeginInvoke(new OneArgIntDelegate(updateProgressBar), e.ProgressPercentage);
      if (e.UserState != null)
      {
          progressBar.Dispatcher.BeginInvoke(new OneArgStrDelegate(updateStatusBar), e.UserState.ToString());
      }
  }
  
  private void updateStatusBar(string Text)
  {
      this.statusBar.Text = Text;
  }
  
  private void updateProgressBar(int ProgressPercentage)
  {
      this.progressBar.Visibility = Visibility.Visible;
      this.progressBar.Value = ProgressPercentage;
  }
  </code></pre>
  
  <p>This solution worked but I thought the whole point of the BackgroundWorker was that I didn't have to do this. Can someone explain my incorrect assumption and what is really going on. Is there a way to do this WITHOUT the dispatcher by setting up the timer differently?</p>
  
  <p>Thanks,</p>
  
  <p>Harrison</p>