title: Passing Background Worker to Another Class in a VSTO Excel Add In
body: <p>I am building a VSTO Excel Add In for Excel 2010... I have a form that the user selects a query and then the populates the sheet appropriately from a table. </p>
  
  <p>I want to report progress as the operation is happening. The operation exists in a seperate class than the UI. Thus I construct the class (ol and ole) and call the method within the <code>DoWork</code> handler. </p>
  
  <pre><code>private void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e)
      {
          BackgroundWorker worker = (BackgroundWorker)sender;
  
          if (!_isExtended)
          {
              oneline ol = new oneline();
              ol.buildOneline(this._wheres,worker );
          }
          else
          {
              ExtendedOneline ole = new ExtendedOneline();
              ole.buildExtendedOneline(this._wheres,worker);
          }
          //worker.ReportProgress();
          e.Result = "COMPLETE!";
      }
  
      private void backgroundWorker3_ProgressChanged(object sender, ProgressChangedEventArgs e)
      {
          this.progressBar1.Value = e.ProgressPercentage;
  
      }
  
      private void backgroundWorker3_WorkerCOmpleted(object sender, RunWorkerCompletedEventArgs e)
      {
          MessageBox.Show(e.Result.ToString());
          this.Close();
      }
  </code></pre>
  
  <p>Then inside the class, specifically the method i am using has a argument set for  <code>BackgroundWorker bw</code>.  This is how i pass in the worker. Then when appropriate i intend to Update the progress by calling <code>bw.ReportProgress(PercentComplete)</code></p>
  
  <pre><code>public void buildOneline(List&lt;WhereStatement&gt; wheres, BackgroundWorker bw)
  {
      ....
      ....
      double count = joinedWells.Count;
      double i = 1;
      foreach (var well in joinedWells)
      {
          double PercentComplete = 100 * (i / count);
          bw.ReportProgress((int)PercentComplete);
          ....
          ....
          i++;
      }
  </code></pre>
  
  <p>This script runs successfully, however the progress bar is not changed at all... Thoughts? </p>
  
  <p><strong>EDIT</strong> </p>
  
  <pre><code>    // backgroundWorker3
      this.backgroundWorker3.WorkerReportsProgress = true;
      this.backgroundWorker3.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker3_ProgressChanged);
      this.backgroundWorker3.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker3_DoWork);
  </code></pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <pre><code>//progressBar1
  this.progressBar1.Location = new System.Drawing.Point(10, 340);
  this.progressBar1.Name = "progressBar1";
  this.progressBar1.Size = new System.Drawing.Size(583, 20);
  this.progressBar1.Step = 1;
  this.progressBar1.TabIndex = 15;
  this.progressBar1.Maximum = 100;
  this.progressBar1.Minimum = 0;
  </code></pre>
  
  <p><strong>ERROR</strong>
  I am now seeing this error that i was not seeing before. </p>
  
  <p>`An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll</p>
  
  <p>Additional information: Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on.`</p>