将值从BackgroundWorker DoWork传递给BackgroundWorker已完成

时间:2013-07-30 00:26:19

标签: vb.net variables backgroundworker pass-by-value byval

如何将BackgroundWorker DoWork的值传递给BackgroundWorker Completed?由于BackgroundWorker Completed未调用BackgroundWorker DoWork,因此除了声明public variable之外,我不知道如何执行此操作。基本上,我希望BackgroundWorker Completed通过ByVal来自BackgroundWorker DoWork的变量接受。{/ p>

1 个答案:

答案 0 :(得分:2)

当您声明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中的RunWorkerCompletedEventArgse.Result以及DoWorkEventArgs中的e.Result = myResult - 后者传递给RunWorkerCompleted前者在方法完成时在你的worker方法结束时设置:

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

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

{{1}}

参考:

RunWorkerCompletedEventArgs.Result Property : MSDN

DoWorkEventArgs.Result Property : MSDN