如何将BackgroundWorker
DoWork
的值传递给BackgroundWorker
Completed
?由于BackgroundWorker
Completed
未调用BackgroundWorker
DoWork
,因此除了声明public variable
之外,我不知道如何执行此操作。基本上,我希望BackgroundWorker
Completed
通过ByVal
来自BackgroundWorker
DoWork
的变量接受。{/ p>
答案 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
中的RunWorkerCompletedEventArgs
,e.Result
以及DoWorkEventArgs
中的e.Result = myResult
- 后者传递给RunWorkerCompleted
前者在方法完成时在你的worker方法结束时设置:
if e.Result = (whatever) then
.... etc
然后在您的{{1}}处理程序中,您也可以通过以下方式访问它:
{{1}}
参考: