title: Background worker in windows form application
body: <p>I have the following constellation:</p>
  
  <p>MainForm.cs -> Including all my Form Elements</p>
  
  <p>Program.cs -> includes the main part, which is a xmlreader/writer to alter xml attributes in xml files that can be as large as 4gb</p>
  
  <p>So this little app works but of course the UI gets unresponsive and freezes which I want to avoid, I also hope to reduce the duration of this process on the way</p>
  
  <p>I start the call of my xmlread/write method from a BtnClick event:</p>
  
  <pre><code>    void BtnApplyChangesClick(object sender, EventArgs e)
      {
          Program p = Program.Instance;
          pbApplyChanges.Minimum = 0;
          pbApplyChanges.Step = 1;
  
          Cursor.Current = Cursors.WaitCursor;
          foreach(DataGridViewRow cr in dataGridView2.Rows)
          {
              pbApplyChanges.Maximum = dataGridView2.Rows.Count;
              p.changeElements(cr.Cells["Filename"].Value.ToString(), txtTenant.Text, txtDate.Text, txtEvtId2.Text);
              pbApplyChanges.PerformStep();
          }
          Cursor.Current = Cursors.Arrow;
          MessageBox.Show("Job done");
      }
  </code></pre>
  
  <p>In the call I use my singleton instance of Program.cs and my main Method there (changeElements) uses 4 String params, that are all taken from information in the Form! (I suppose this is kinda bad practice but it worked so far...)</p>
  
  <p>When I tried to replace this method call with a backgroundWorker (itself made the method call then) I failed as the method call wasn't even made... I found out that UI elements can't be accessed from the BW thread, so I suppose this is also the reason for my method call not working?!</p>
  
  <p>So how can I get this constellation to work? Do I have to pass all 4 string Params AND the class instance (of Program.cs) to the background worker? Is BW even the best tool for the job?</p>