相关疑难解决方法(0)

为什么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();
}
Run Code Online (Sandbox Code Playgroud)

请注意,我添加了一个Dispatcher.Invoke,等到bw不忙,但如果我调用它并且从不触发RunWorkerAsyncCompleted事件,则所有时间都很忙.虽然,如果我删除该行,FIRES事件,这真的很奇怪.

我怎么能等到bw结束?

c# wpf dispatcher backgroundworker dispatch

9
推荐指数
2
解决办法
2万
查看次数

标签 统计

backgroundworker ×1

c# ×1

dispatch ×1

dispatcher ×1

wpf ×1