title: .NET BackGroundWorker - InvalidOperationException : Cross-thread operation not valid
body: <p>I have a project coded in .NET Winforms.
  I need to implement a data-mining operation, print the text to TextBox and update the progress. </p>
  
  <p>I tried to use BackgroundWorker to do, but it throws a InvalidOperationException (<strong>Cross-thread operation not valid: Control ‘xxxxx’ accessed from a thread other than the thread it was created on</strong>)</p>
  
  <p>To narrow down the potential causes of the problem, I started a new project, included the following:
  Button -  To start the BackgroundWorker
  Label - to print the text.
  And ProgressBar.</p>
  
  <p>However, the result is the same. I searched on SOF, and was told to use a delegate, but I  am not familiar with it.</p>
  
  <p>This is the code sample that throws the error:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  
  namespace TestProject
  {
      public partial class Form1 : Form
      {
          private readonly BackgroundWorker _bw = new BackgroundWorker();
  
          public Form1()
          {
              InitializeComponent();
              _bw.DoWork += RosterWork;
              _bw.ProgressChanged += BwProgressChanged;
              _bw.RunWorkerCompleted += BwRunWorkerCompleted;
              _bw.WorkerReportsProgress = true;
              _bw.WorkerSupportsCancellation = false;
          }
  
          private void RosterWork(object sender, DoWorkEventArgs doWorkEventArgs)
          {
              for (int i = 0; i &lt; 1000; i++)
              {
                  label1.Text = i.ToString();
                  _bw.ReportProgress(Convert.ToInt32((i * (100 / 1000))));
              }
          }
  
          private void BwProgressChanged(object sender, ProgressChangedEventArgs e)
          {
              progressBar1.Value = e.ProgressPercentage;
          }
  
          private void btnStart_Click(object sender, EventArgs e)
          {
              progressBar1.Show();
              _bw.RunWorkerAsync();
          }
  
          private void BwRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
          {
              progressBar1.Hide();
          }
  
      }
  }
  </code></pre>
  
  <p><strong>Update:</strong>
  I follow Jon Skeet's answer, it really work on my test project, but back to my real project,</p>
  
  <p>The Layout of my Form:</p>
  
  <p>Form -
     TabControl
       - Tab1 
         -Tab1Panel
         -TextBox1</p>
  
  <p>When reach this line :</p>
  
  <pre><code>TextBox txtbox1 = new TextBox();
  Tab1Panel.Controls.Add(txtbox1);
  </code></pre>
  
  <p>The error still occur when i add Textbox to Panel Control Programmatically.</p>
  
  <p>Finally,I replace by this:</p>
  
  <pre><code> if (Tab1Panel.InvokeRequired)
       Tab1Panel.Invoke((MethodInvoker)delegate { Tab1Panel.Controls.Add(txtbox1); });
   else
       Tab1Panel.Controls.Add(txtbox1);
  </code></pre>
  
  <p>Everything is work. How to determine the control is InvokeRequired, Is it control specified?</p>