Tag: 员调度

为什么BackgroundWorker总是很忙?

我在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(); } 请注意,我添加了Dispatcher.Invoke以等待bw不忙,但如果我调用它并且从不触发RunWorkerAsyncCompleted事件,则所有时间都很忙。 虽然,如果我删除该行,FIRES事件,这真的很奇怪。 我怎么能等到bw结束?