title: How to make BackgroundWorker ProgressChanged events execute in sequence?
body: <p>Consider the following code:</p>
  
  <pre><code>private static BackgroundWorker bg = new BackgroundWorker();
  
  static void Main(string[] args) {
    bg.DoWork += bg_DoWork;
    bg.ProgressChanged += bg_ProgressChanged;
    bg.WorkerReportsProgress = true;
    bg.RunWorkerAsync();
  
    Thread.Sleep(10000);
  }
  
  static void bg_ProgressChanged(object sender, ProgressChangedEventArgs e) {
    Console.WriteLine(e.ProgressPercentage);
    Thread.Sleep(100);
    Console.WriteLine(e.ProgressPercentage);
  }
  
  static void bg_DoWork(object sender, DoWorkEventArgs e) {
    for (int i = 0; i &lt; 10; i++) {
      bg.ReportProgress(i);
    }
  }
  </code></pre>
  
  <p>When run I get the following output:</p>
  
  <blockquote>
    <p>0
    1
    1
    2
    0
    3
    2
    3
    5
    4
    4
    6
    5
    7
    7
    8
    6
    9
    8
    9</p>
  </blockquote>
  
  <p>I understand that the issue is a race condition between the threads that BackgroundWorker starts for each call to ReportProgress. </p>
  
  <p>How can I make sure that the whole body of each bg_ProgressChanged gets executed in the order I have called them?
  That is I would like to get </p>
  
  <blockquote>
    <p>0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9</p>
  </blockquote>
  
  <p>as a result.</p>