3

如何将值从BackgroundWorker DoWorkto传递BackgroundWorker Completed?由于BackgroundWorker Completed不被调用,因此BackgroundWorker DoWork我不确定如何执行此操作,除非声明一个public variable. 本质上,我想BackgroundWorker Completed通过ByVal来自BackgroundWorker DoWork.

4

1 回答 1

3

声明DoWork函数时,请注意它内置了一些方便的参数:

Private Sub backgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) _
                                                  Handles backgroundWorker1.DoWork

并注意RunWorkerCompleted处理程序的类似参数:

Private Sub backgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _
                                          ByVal e As RunWorkerCompletedEventArgs) _
                                       Handles backgroundWorker1.RunWorkerCompleted

至关重要的是,您可以访问e.Result,它可以是任何对象,在RunWorkerCompletedEventArgs,也可以e.Result在您的DoWorkEventArgs- 后者在方法完成时传递给前者,因此在您的工作方法结束时只需设置:

e.Result = myResult

然后在您的RunWorkerCompleted处理程序中,您也可以通过以下方式访问它:

if e.Result = (whatever) then 
    .... etc

参考 :

RunWorkerCompletedEventArgs.Result 属性:MSDN

DoWorkEventArgs.Result 属性:MSDN

于 2013-07-30T00:46:33.407 回答