title: C# BackgroundWorker ProgressChanged doesn't get fired until end of function
body: <p>I have a method in my class that has some loops inside.
  Main purpose of this method is converting some files so I put a progressbar in my form that should get updated after each file has been converted.</p>
  
  <p>I tried every possible combination and I read everything I could but I couldn't solve this issue.</p>
  
  <pre><code>void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
  {
      converterProgressBar.Value = e.ProgressPercentage;
  }
  </code></pre>
  
  <p>is called only after the main loop of my method has been executed.</p>
  
  <p>This is my method:</p>
  
  <pre><code>public string Convert()
  {
      convertBtn.Enabled = false;
      bw.WorkerReportsProgress = true;
      bw.WorkerSupportsCancellation = true;
  
      bw.DoWork += new DoWorkEventHandler(bw_DoWork);
      bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
      bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
      totalCount = files.length;
      bw.RunWorkerAsync();
  
      if (!Directory.Exists(folder))
      {
          Directory.CreateDirectory(folder);
      }
      foreach (string file in files)
      {
          countFile++;
          if (chk.Checked)
          {
              class1.DoJob();
          }
  
          using (// some code))
          {
              using (//some other code))
              {
                  try
                  {
                      using (// again some code)
                      {
                          // job executing
                      }
                  }
                  catch (exception
                  {
  
                  }   
              }
          }
          convertedVideosL.Text = txtToUpdate;
          convertedVideosL.Refresh();
      }
      countFile = countFile + 1;
      MessageBox.Show("Done");
      countFile = -1;
      return outputFile;
  }
  </code></pre>
  
  <p>And here are the <code>BackgroundWorker</code> Event Handlers:</p>
  
  <pre><code>void bw_DoWork(object sender, DoWorkEventArgs e)
  {
      for (int i = 0; i &lt;= totalCount; i++)
      {
          if (bw.CancellationPending)
          {
              e.Cancel = true;
          }
          else
          {
              int progress = Convert.ToInt32(i * 100 / totalCount);
              (sender as BackgroundWorker).ReportProgress(progress, i);
          }
      }
  }
  
  void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
  {
      converterProgressBar.Value = e.ProgressPercentage;
  }
  
  void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  {
      if (e.Cancelled == false)
      {
          convertedVideosL.Text = "Finished!";
      }
      else
      {
          convertedVideosL.Text = "Operation has been cancelled!";
      }
  }
  </code></pre>
  
  <p>But I cannot get to update the progress bar for every file that is converted.
  It waits for the foreach loop to end and then calls <code>bw_ProgressChanged</code>.</p>
  
  <p>If I put <code>RunWorkerAsync()</code> inside the foreach loop an exception is thrown that says the <code>BackgroundWorker</code> is busy and cannot execute other tasks.</p>
  
  <p>It seems to me obvious that <code>DoWork()</code> only executes a for loop then it shouldn't be aware of the conversion going on but <code>ProgressChanged</code> should be fired by <code>ReportProgress(progress,i)</code>.</p>
  
  <p>Could please someone explain me why and help me with a solution?</p>
  
  <p>Thanks!</p>