0

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?

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: " & BackgroundWorker1.CancellationPending)
    Delay(5)
    ListBox1.Items.Add("Busy: " & 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

End Class

3
  • 2
    Move (or copy) the check for the cancellation inside the loop
    – Steve
    Mar 22 '16 at 16:05
  • Wow... can't believe I missed that... thanks Steve!
    – HurstOlds
    Mar 22 '16 at 16:06
  • @HurstOlds What happens when you click your button again? If BackgroundWorker1.Dispose() that is in place... I know what happens :)
    – Trevor
    Mar 22 '16 at 16:13
1

You just need to move the check for the cancellation inside the Do...Loop otherwise it will be tested only at the start of the DoWork event handler and never after that

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()
    Else
        Do
          If BackgroundWorker1.CancellationPending = True Then
            e.Cancel = True
            BackgroundWorker1.Dispose()
            Exit Do
          Else 
             Measurement = Now()
          End If
        Loop
    End if
End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.