WPF ProgressBar 控件调度仍然阻塞 UI 线程
我正在使用 WPF 尝试在后台运行一个线程来更新进度栏。我不想阻止 UI 线程,因此我运行以下代码。然而,UI 仍然被阻止。看起来很简单,我做错了什么?
Dispatcher.BeginInvoke(
(ThreadStart) delegate(){
for(double i = progressBar_ChangeProgress.Minimum;
i < progressBar_ChangeProgress.Maximum;
i++)
{
for (int b = 0; b < 100000000; b++) { }
progressBar_ChangeProgress.Value = i;
}
EnableAllInputControls();
}, DispatcherPriority.Background);
I am using WPF trying to run a Thread in the background that updates a progress bar. I don't wan to block the UI thread so I am running the following code. However the UI still blocked. It seems so simple, what am I doing wrong?
Dispatcher.BeginInvoke(
(ThreadStart) delegate(){
for(double i = progressBar_ChangeProgress.Minimum;
i < progressBar_ChangeProgress.Maximum;
i++)
{
for (int b = 0; b < 100000000; b++) { }
progressBar_ChangeProgress.Value = i;
}
EnableAllInputControls();
}, DispatcherPriority.Background);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下为什么不利用
BackgroundWorker
...更新:
如果您想使用
Dispatcher
;将优先级设置为Normal
并在后台线程上执行处理,然后调用 UI 线程上的方法来提供更新。Why not leverage the
BackgroundWorker
in this scenario...UPDATE:
If you are wanting to use the
Dispatcher
; set the priority toNormal
and perform the processing on the background thread then calling a method on the UI thread to provide the update.Dispatcher
只是一种稍后在 UI 线程上运行一些代码的机制。您传入的优先级控制何时它将被执行,而不是任何类型的线程优先级。在这种情况下,委托的内容正在 UI 线程上运行。使用 Aaron 提到的 BackgroundWorker 肯定会有所帮助。我还可能指出,通常进度条会显示任务距离完成的程度。如果您不知道某件事需要多长时间或无法衡量进度,则可以使用不确定进度条。仅当您有一些有意义的信息时才更新该值。 (尽管您可能只是出于演示目的提供了此信息)
The
Dispatcher
is just a mechanism to run a bit of code on the UI thread at a later time. The priority you're passing in controls when it will get executed, not any type of thread priority. The contents of your delegate in this case are getting run on the UI thread. Using aBackgroundWorker
as mentioned by Aaron would certainly help here.Also I might point out that usually a progress bar shows how close a task is to completing. If you don't know how long something is going to take or have no way of measuring progress, you can use an Indeterminate progress bar. Only update the value if you have some meaningful information. (though you may have just provided this for demonstration purposes)
BeginInvoke 中的所有内容都在 UI 线程上运行,因此它将阻塞。
您需要做的是在线程中运行任何时间密集型代码,然后只需更新 Invoke 内的 UI。
你想要更多这样的东西:
Everything inside BeginInvoke is being run on the UI thread and so it will block.
What you need to do is run any time intensive code in your thread and then just update the UI inside the Invoke.
You want something more like this: