title: Is CancelAsync working or not?
body: <p>I've made a small app where <code>Form</code> is threaded (using <code>BackgroundWorker</code>), and in the form I'm calling a function <code>QuitApplication</code> in <code>Program</code> class when I want to quit.</p>
  
  <p>The <code>DoWork</code> looks like this:</p>
  
  <pre><code>static void guiThread_DoWork(object sender, DoWorkEventArgs e)
  {
      BackgroundWorker worker = sender as BackgroundWorker;
  
      while (true)
      {
          if (worker.CancellationPending == true)
          {
              e.Cancel = true;
              break;
          }
  
          if (Program.instance.form != null)
          {
              Program.instance.form.UpdateStatus(Program.instance.statusText, Program.instance.statusProgress);
          }
  
          Thread.Sleep(GUI_THREAD_UPDATE_TIME);
      }
  }
  </code></pre>
  
  <p>and in the Form1 class i have this method attached to the closing of the window:</p>
  
  <pre><code>void Form1_FormClosed(object sender, FormClosedEventArgs e)
  {
      Program.instance.SetStatus("Closing down...", 0);
  
      Program.QuitApplication();
  }
  </code></pre>
  
  <p>So what i want is to ensure that everything quits when I press the X on the window. However, the <code>if( worker.CancellationPending == true )</code> never hits... why is this?</p>
  
  <p>QuitApplication looks like this:</p>
  
  <pre><code>public static void QuitApplication()
  {
      Program.instance.guiThread.CancelAsync();
  
      Application.Exit();
  }
  </code></pre>
  
  <p>And Im using <code>guiThread.WorkerSupportsCancellation = true</code></p>