VBNet如何知道后台工作者是否已被取消

时间:2011-12-30 03:54:08

标签: vb.net backgroundworker

目前,我有这样的代码: My_BgWorkerB 是我的BackGround工作者的名字

    If My_BgWorkerB.IsBusy Then
        If My_BgWorkerB.WorkerSupportsCancellation Then
            My_BgWorkerB.CancelAsync()
        End If

        ' in this part i want to know if the background worker is already stopped
        ' so that i can start it again

        ' My_BgWorkerB.RunWorkerAsync() ' => this should be triggered if the worker
                                        '    has already been stop.

    Else
        My_BgWorkerB.RunWorkerAsync()
    End If

1 个答案:

答案 0 :(得分:0)

您应该检查BGW的RunWorkerCompletedEventArgs事件处理程序中的RunWorkerCompleted Cancelled属性。如果是真的,请使用RunWorkerAsync()方法重新启动BGW。

这样的事情:

Private Sub My_BgWorkerB_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles My_BgWorkerB.RunWorkerCompleted
    If e.Cancelled Then
       My_BgWorkerB.RunWorkerAsync()  
    End If
End Sub