2011-12-30 70 views
-1

目前,我有这样的代码:My_BgWorkerB是我的后台工作的名称VBNet如何知道,如果后台工作已经取消,

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 

回答

0

你应该做的是检查您的BGW年代RunWorkerCompletedEventArgsCancelled财产RunWorkerCompleted事件处理程序。如果确实如此,请使用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 
+0

谢谢你,它的工作。 – 2011-12-30 05:22:32

+0

如果我关闭表单并且工作人员仍然很忙,该怎么办?它会抛出一个错误? – 2011-12-30 06:49:50

+0

@johntotetwoo不,BGW将随着BGW'拥有'的表格关闭而停止。这本身不会抛出任何例外。但是,如果您需要在BGW的RunWorkerCompleted事件处理程序中运行代码,即使用户在运行BGW上关闭了表单,也可以使用表单的FormClosing事件来调用BGW的CancelAsync()[说实话,我不确定是否在BGW有机会取消之前表单会关闭,或者如果您需要取消FormClosing事件处理程序中关闭的表单并关闭BGW RunWorkerCompleted处理程序 - 请告诉我您是否需要在此处进行说明。 – 2011-12-30 16:52:28

相关问题