title: ShowDialog in BackgroundWorker RunWorkerCompleted
body: <p>I'm working on a WinForms application that is launching several background workers. When one background worker is complete, if the result is a failure, it will show a dialog box via the <code>ShowDialog(this)</code> method. The problem is when multiple background worker results are fail, it will show multiple dialogs at the same time. I didn't think this was possible, but it apparently is. I was reading some stuff about the the Message Loop and it seems that even though a dialog is open, the message loop is still processing messages, which means that the runworkercompleted will be called even if a dialog is open already. I though that I could use a "lock (myObject)" on the dialog, but it appears that it does not, I'm guessing since that same thread is calling the lock each time.</p>
  
  <p>So what is the appropriate way to fix this? I'm half tempted to just use a flag and a loop like this:</p>
  
  <pre><code>public bool dialogOpen = false;
  public bool cancelMessages = false;
  public void x_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  {
      while (dialogOpen) {}
      if (cancelMessages) return;
      dialogOpen = true;
      MyDialog dlg = new MyDialog("Something went wrong.");
      if (dlg.ShowDialog(this) == DialogResult.Cancel) cancelMessages = true;
      dialogOpen = false;
  }
  </code></pre>
  
  <p>Would this even work? Would this cause other bad things to happen? (Would this block the message loop?)</p>