title: How close BackgroundWorker thread when application is deactivated?
body: <p>I create thread with <code>BackgroundWorker</code>, and in the loop I check every time if <code>CancellationPending</code> is true or not, like this:</p>
  
  <pre><code>   public MainPage()
      {
          InitializeComponent();
  
          bw = new BackgroundWorker();
          bw.WorkerReportsProgress = true;
          bw.WorkerSupportsCancellation = true;
          bw.DoWork += new DoWorkEventHandler(bw_DoWork);
          bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
          bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
      }
  
      private void ButtonStart_Click(object sender, RoutedEventArgs e)
      {
          if (bw.IsBusy != true)
          {
              bw.RunWorkerAsync();
          }
      }
  
      private void ButtonCancel_Click(object sender, RoutedEventArgs e)
      {
          if (bw.WorkerSupportsCancellation)
          {
              bw.CancelAsync();
          }
      }
  
      private void bw_DoWork(object sender, DoWorkEventArgs e)
      {
          BackgroundWorker worker = sender as BackgroundWorker;
  
          for (int i = 1; i &lt;= 100; i++)
          {
              Debug.WriteLine("The tread is working");
              if (worker.CancellationPending)
              {
                  e.Cancel = true;
                  bw.CancelAsync();
                  break;
              }
              else
              {
  
                  System.Threading.Thread.Sleep(500);
                  worker.ReportProgress(i);
              }
          }
      }
  
      private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
      {
          if (e.Cancelled)
          {
              tbProgress.Text = "Canceled";
          }
          else if (e.Error != null)
          {
              tbProgress.Text = "Error: " + e.Error.Message;
          }
          else
          {
              tbProgress.Text = "Done";
          } 
      }
  
      private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
      {
          tbProgress.Text = e.ProgressPercentage.ToString() + "%";
      }
  </code></pre>
  
  <p>When application is deactivated, the thread wasn't closed, it is aborted and the exception occurs. How close threads with <code>BackgroundWorker</code> when application is deactivated?</p>