title: C# background worker, winform. "Cross-thread operation not valid:"
body: <p>I'm trying to get background worker functioning in the most basic way with a windows form, for instance get a background process to change text in a label.. I got the basic background worker code here.. <a href="http://www.albahari.com/threading/part3.aspx" rel="nofollow">http://www.albahari.com/threading/part3.aspx</a>
  Heres the code in my form.. Trying to make it so you press a button and then background worker thread is spawned which changes text in the label </p>
  
  <pre><code>    using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
  
         namespace WindowsFormsApplication4
      {
          public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
              }
  
  
              BackgroundWorker _bw = new BackgroundWorker();
  
               void backgroundio()
              {
                  _bw.DoWork += bw_DoWork;
                  _bw.RunWorkerAsync("Message to worker");
  
              }
  
               void bw_DoWork(object sender, DoWorkEventArgs e)
              {
                  // This is called on the worker thread
                  label1.Text = (string)(e.Argument);        // writes "Message to worker"
                  // Perform time-consuming task...
              }
  
               void button1_Click(object sender, EventArgs e)
               {
                   backgroundio();
               }
  
          }
      }
  </code></pre>
  
  <p>For the  label1.Text = (string)(e.Argument); I get this error.</p>
  
  <p>Cross-thread operation not valid: Control 'label1' accessed from a thread other than the thread it was created on.</p>
  
  <p>Thank for any help !!  :)</p>
  
  <p>Actually while I'm here can somebody explain this line?</p>
  
  <pre><code> _bw.DoWork += bw_DoWork;
  </code></pre>
  
  <p>I dont get how += makes any sense in this context. how can you add those things?</p>