Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 无法取消BackgroundWorker进程_Vb.net_Backgroundworker_Cancellation - Fatal编程技术网

Vb.net 无法取消BackgroundWorker进程

Vb.net 无法取消BackgroundWorker进程,vb.net,backgroundworker,cancellation,Vb.net,Backgroundworker,Cancellation,我有一个BackgroundWorker,它包含一个类ExcelOutput,用于将各种数据输出到工作簿,我应该马上提到设置了bw.WorkerSupportsCancellation=True 在输出的每个阶段,我都会使用Try/Catch检查ExcelOutput中的错误,必要时还会显示错误(使用名为ErroReport()的函数) 结合错误消息,我想取消BackgroundWorker以避免进一步的错误。为此,我已将OutputWorker属性添加到ExcelOutput类中,并将其设置为

我有一个
BackgroundWorker
,它包含一个类
ExcelOutput
,用于将各种数据输出到工作簿,我应该马上提到设置了
bw.WorkerSupportsCancellation=True

在输出的每个阶段,我都会使用
Try/Catch
检查
ExcelOutput
中的错误,必要时还会显示错误(使用名为
ErroReport()
的函数)

结合错误消息,我想取消
BackgroundWorker
以避免进一步的错误。为此,我已将
OutputWorker
属性添加到
ExcelOutput
类中,并将其设置为
bw_DoWork()
方法中我的
BackgroundWorker
的副本

但是,在
ExcelOutput.ErrorReport()
中执行的取消不起作用,我不知道为什么

请注意,我已经测试了
bw.CancellationPending
的值,并在出现错误后将其设置为
True
。我还测试了下面的
If
条件是否通过显示消息框起作用,并且也起作用。出于某种原因,似乎忽略了
Exit Sub
命令

有人能告诉我我做错了什么吗?谢谢

下面是如何设置
BackgroundWorker
类中的
bw\u DoWork()
函数-

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
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
下面是如何设置
ExcelOutput
类中的
ErrorReport()
函数-

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
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
Private子错误报告(ByVal Ex作为例外,
可选的ByVal CustomMessage(字符串=”)
调用我。ResetRange()'销毁“Range”对象
Dim ErrorMessage As String=“Message:&Ex.Message”设置默认消息
如果是CustomMessage“”,则ErrorMessage=CustomMessage&vbCrLf&vbCrLf&Ex.Message
将结果调整为整数=MessageBox.Show(ErrorMessage,
“发生错误”,
MessageBoxButtons.OK,
MessageBoxIcon.Stop)
“**关闭工作簿(如果打开)并停止OutputWorker*”
尝试
Call Me.WB.Close(SaveChanges:=False)
如果Me.OutputWorker.workerSupportsScanCellation=True,则
Me.OutputWorker.CancelAsync()
如果结束
抓住
结束尝试
端接头

您应该尝试将DoWorkEventsArgs作为参数添加到ErrorReport函数中

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

您可以取消Backgroundworker。

谢谢,但这样我也能得到同样的结果。