我在WPF应用程序中的后台工作者中意识到了一些奇怪的东西.
我现在想要完成的是等到BW完成另一个线程.
检查以下代码:
if (bw.IsBusy)
{
bw.CancelAsync();
System.Threading.ThreadStart WaitThread =
new System.Threading.ThreadStart(delegate() {
while (bw.IsBusy)
{
System.Threading.Thread.Sleep(100);
}
bw.RunWorkerAsync();
});
System.Windows.Application.Current.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
WaitThread); // if I remove this line, bw fires RunWorkerAsyncEvent
}
else
{
bw.RunWorkerAsync();
}
Run Code Online (Sandbox Code Playgroud)
请注意,我添加了一个Dispatcher.Invoke,等到bw不忙,但如果我调用它并且从不触发RunWorkerAsyncCompleted事件,则所有时间都很忙.虽然,如果我删除该行,FIRES事件,这真的很奇怪.
我怎么能等到bw结束?