title: vb.net background worker cancel not working
body: <p>I'm having an issue where BackgroundWorker.CancelAsync() isn't working. I have WorkerSupportsCancellation set to TRUE. I am also polling BackgroundWorker1.CancellationPending in DoWork. Here is sample code of what I am trying to achieve. I have background worker looping through a time stamp and assigning a value to Measurement variable. I have a subroutine that queries the last reported Measurement variable and writes to listbox. After 5 loops I send BackgroundWorker.CancelAsync(). I can see the cancellation is pending, but it doesn't actually cancel the background worker. Is this a race condition?</p>
  
  <pre><code>Public Class Form1
  
  Dim Measurement As String
  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  
      BackgroundWorker1.RunWorkerAsync()
      Delay(0.5)
  
      For x = 1 To 5
          ListBox1.Items.Add(Measurement)
          ListBox1.TopIndex = ListBox1.Items.Count - 1
          TextBox1.Text = BackgroundWorker1.CancellationPending
          Delay(1)
      Next
  
      BackgroundWorker1.CancelAsync()
  
      TextBox1.Text = BackgroundWorker1.CancellationPending
      ListBox1.Items.Add("Cancel Pend: " &amp; BackgroundWorker1.CancellationPending)
      Delay(5)
      ListBox1.Items.Add("Busy: " &amp; BackgroundWorker1.IsBusy)
  
  End Sub
  
  Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
  
      If BackgroundWorker1.CancellationPending = True Then
          e.Cancel = True
          BackgroundWorker1.Dispose()
          Exit Sub
      Else
          Do
              Measurement = Now()
          Loop
      End If
  
  End Sub
  </code></pre>
  
  <p>End Class</p>