Cannot cancel a BackgroundWorker procss

102 Views Asked by At

I have a BackgroundWorker that includes a class ExcelOutput, used to output various data to a workbook, and I should mention straight away that bw.WorkerSupportsCancellation = True is set.

At each stage of the output I'm checking for errors in ExcelOutput using Try/Catch, and if necessary displaying an error (using a function called ErroReport().

In conjunction with the error message, I want to cancel the BackgroundWorker to avoid further errors. To that end I have added the OutputWorker property to the ExcelOutput class and I set that to be a copy of my BackgroundWorker in the bw_DoWork() method.

However, the cancellation carried out in ExcelOutput.ErroReport() is not working, and I don't know why.

Note that I've tested the value of bw.CancellationPending and it is set to True after an error. I've also tested that the If condition following is working by showing a message box, and that also works. For some reason it seems as though the Exit Sub command is ignored though.

Can anyone suggest what I am doing wrong? Thanks.

Here is how the bw_DoWork() function from the BackgroundWorker class is set up -

Private Sub bw_DoWork(ByVal sender As Object,
                      ByVal e As DoWorkEventArgs)

    Dim Excel As New ExcelOutput  ' Create a new instance of the ExcelOutput class
    Dim CurrentRow As Integer = 4    ' Set the first output row

    '** Include a copy of the OutputWorker in the ExcelOutput (so that the OutputWorker can be cancelled)
    Excel.OutputWorker = Me

    If bw.CancellationPending = True Then
        e.Cancel = True
        Exit Sub
    Else
        Excel.Prepare()
    End If

    If bw.CancellationPending = True Then
        e.Cancel = True
        Exit Sub
    Else
        CurrentRow = Excel.OutputGroup("General", Headers, Data, 4)
    End If

    ' More stuff here...

End Sub

Here is how the ErrorReport() function from the ExcelOutput class is set up -

Private Sub ErrorReport(ByVal Ex As Exception,
                        Optional ByVal CustomMessage As String = "")

    Call Me.ResetRange()    ' Destroy the 'Range' object
    Dim ErrorMessage As String = "Message: " & Ex.Message ' Set the default message
    If CustomMessage <> "" Then ErrorMessage = CustomMessage & vbCrLf & vbCrLf & Ex.Message
    Dim Result As Integer = MessageBox.Show(ErrorMessage,
                                            "An Error Has Occured",
                                            MessageBoxButtons.OK,
                                            MessageBoxIcon.Stop)

    '** Close the workbook (if it's open) and stop the OutputWorker *'
    Try
        Call Me.WB.Close(SaveChanges:=False)
        If Me.OutputWorker.WorkerSupportsCancellation = True Then
            Me.OutputWorker.CancelAsync()
        End If
    Catch
    End Try

End Sub
1

There are 1 best solutions below

1
On

You should try to add the DoWorkEventsArgs as parameter to your ErrorReport function.

Private Sub ErrorReport(ByVal Ex As Exception,
                    Optional ByVal CustomMessage As String = "",
                    ByVal e As DoWorkEventsArgs)

Call Me.WB.Close(SaveChanges:=False)
    If e.WorkerSupportsCancellation = True Then
        e.CancelAsync()
    End If

You'll be able to cancel the Backgroundworker.