title: C# backgroundworker RunworkerCompleted vs async await
body: <p>Updated with answers:
  The true way of wait until a number of different tasks to be finished would need async await instead of background worker.</p>
  
  #
  
  <p>I know there are numerous discussion about backgroundworker but I've being searched around and cannot find the answer. </p>
  
  <p>Here is my code example(basic logic, the actual code is much longer), I wonder if there is a way to get around this:</p>
  
  <pre><code> BackgroundWorker MCIATS1Worker = new BackgroundWorker();
      private AutoResetEvent _MCIATS1WorkerResetEvent = new AutoResetEvent(false);
  
      public MainWindow()
      {
          InitializeComponent();
  
          MCIATS1Worker = new BackgroundWorker();
          MCIATS1Worker.DoWork += new DoWorkEventHandler(MCIATS1Worker_DoWork);
          MCIATS1Worker.WorkerReportsProgress = true;
          MCIATS1Worker.WorkerSupportsCancellation = true;
          MCIATS1Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(MCIATS1_RunWorkerCompleted);
  
          for (int i = 1; i &lt;= 10; i++)
          {
              //some code
              MCIATS1Worker.RunWorkerAsync();
              _MCIATS1WorkerResetEvent.WaitOne();
          }
  
      }
  </code></pre>
  
  <p>DoWork and runworkercompleted</p>
  
  <pre><code>void MCIATS1Worker_DoWork(object sender, DoWorkEventArgs e)
      {
          //do something here
      }
  
      void MCIATS1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
      {
          MessageBox.Show("hello world");
          _MCIATS1WorkerResetEvent.Set();
      }
  </code></pre>
  
  <p>For some reasons, the MCIATS1_RunWorkerCompleted won't be triggered until the loop finished. And apparently the WaitOne is holding the loop.<br>
  Here is my question, </p>
  
  <p>why RunWorkerCompleted won't be trigger the RunWorkerCompleted when the worker is actually finished the work?  </p>
  
  <p>Thank you.</p>
  
  ###UPDATED SOLUTION
  
  <p>This is the right way of doing it.</p>
  
  <pre><code>private async void WhateverFunction()
     {
      await Task.WhenAll(MCIATS1WorkerDoWorkAsync(param),...other tasks);
      }
  
  
  private Task MCIATS1WorkerDoWorkAsync(bkgWorkParameter param)
      {
          return Task.Run(() =&gt;
          {
              //Do whatever
          });
      }
  </code></pre>