BackgroundWorker
组件有一个IsBusy
标志。当它正在执行RunWorkerASync
时,此标志设置为True。在RunWorkerASync
完成或RunWorkerCompleted
完成时,它会更改为False吗?
答案 0 :(得分:0)
IsBusy
标志在RunWorkerASync
执行之前RunWorkerCompleted
结束时将立即设置为false。假设你想从另一个函数中检索BackgroundWorker
操作的结果,如下所示:
Dim MyResult = "Not Set"
Sub Foo()
DoSomeStuff()
RunWorkerASync()
'Wait for worker
While Worker.IsBusy
Wait()
End While
'Use result
'This won't work because MyResult hasn't been
'set yet by the RunWorkerCompleted handler
'Will Print "Not Set", unless, by a timing quirk,
'RunWorkerCompleted has enough time to execute
'Before using its result.
Print MyResult
End Sub
Sub RunWorkerASync()
e.Result = "Result has been set"
End Sub
Sub RunWorkerCompleted()
MyResult = e.Result 'Worker's output
End Sub