title: How do I use backgroundworker in this instance?
body: <p>I've a question on how the backgroundworker would actually work. I'm not too sure how it would work with different methods. </p>
  
  <p>So for example I have the following code (adapted from <a href="http://broadcast.oreilly.com/2010/06/understanding-c-using-backgrou.html" rel="nofollow">http://broadcast.oreilly.com/2010/06/understanding-c-using-backgrou.html</a> for illustration purposes):</p>
  
  <pre><code>private void getDateTime()
  {
      DateTime startTime = DateTime.Now;
      double value = Math.E;
      while (DateTime.Now &lt; startTime.AddMilliseconds(100)) 
      {
        value /= Math.PI;
        value *= Math.Sqrt(2);
      }
  }
  
  private backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  {
      for(int i=0; i &lt;= 100; i++)
      {
          getDateTime();
          backgroundWorker1.ReportProgress(i);
      }
  }
  </code></pre>
  
  <p>This would continually calculate the DateTime using the backgroundworker thread. Now I'm assuming that the for-loop within the backgroundworker's DoWork method would run the calculation, report the progress, and then reiterate the loop (please do correct me if I've misunderstood). </p>
  
  <p>Now assuming I have another method, which parses a (presumably large) DataTable and returns another DataTable, using it in the backgroundworker would mean I'd be parsing the DataTable over and over (theoretically. again, correct me if I'm wrong)? If this is how the program would flow, is there a more efficient method to achieve the DataTable parsing without having to reiterate the loop, but still being able to use backgroundworker? </p>
  
  <p>This is intended for a Windows Form, and the UI tends to freeze up when I parse the DataTable(s). I've heard that backgroundworker could help me get around the freezing of the UI, but I find it highly inefficient to have to re-parse the entire DataTable over, and over. However I'm not sure of any other way I can report progress without a for-loop either.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I've figured out a temporary solution. It works, but I still have no way of reporting progress.</p>
  
  <pre><code>    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
      {
          int rows = (int)e.Argument;
          int count = 0;
          t3 = runComparison();
          for (int i = 0; i &lt;= rows; i++)
          {
              count++;
              int current = count / rows;
              backgroundWorker1.ReportProgress(current * 100);
          }
          e.Result = t3;
      }
  
      private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
      {
          progressBar1.Value = e.ProgressPercentage;
      }
  
      private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
      {
          if (e.Error != null)
          {
              MessageBox.Show("An unexpected error has occurred. Please try again later.", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
          else
          {
              dataGridView1.DataSource = e.Result;
          }
      }
  </code></pre>
  
  <p>I'm aware my for-loop doesn't do anything. However, the "runComparison" already consumes a lot of time. As such, progress doesn't get reported until an entire run-through if it's in the for-loop. Alternatively, maybe I could use a label or messagebox instead of the progress bar. Not too sure how I'm supposed to achieve this. </p>