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。谢谢,但这样我也能得到同样的结果。