WPF BackgroundWorker与分派器 [英] WPF BackgroundWorker vs. Dispatcher

查看:75
本文介绍了WPF BackgroundWorker与分派器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WPF应用程序中,我需要执行异步操作,然后需要更新GUI.这件事我必须在不同的时刻以不同的透明度做很多次.我知道执行此操作的两种方法:Dispatcher和BackgroundWorker.

In my WPF application I need to do an async-operation then I need to update the GUI. And this thing I have to do many times in different moment with different oparations. I know two ways to do this: Dispatcher and BackgroundWorker.

因为当我选择时,我很难回去,所以我问你:有什么更好的?选择一个而不是另一个的原因是什么?

Because when I will chose it will be hard for me to go back, I ask you: what is better? What are the reasons for choosing one rather than the other?

谢谢! 皮莱吉(pileggi)

Thank you! Pileggi

推荐答案

Dispatcher与其他线程方法之间的主要区别是Dispatcher实际上不是多线程的.分派器控制控件,这些控件需要一个线程才能正常运行. Dispatcher的BeginInvoke方法将事件排队以便稍后执行(取决于优先级等),但仍在同一线程上.

The main difference between the Dispatcher and other threading methods is that the Dispatcher is not actually multi-threaded. The Dispatcher governs the controls, which need a single thread to function properly; the BeginInvoke method of the Dispatcher queues events for later execution (depending on priority etc.), but still on the same thread.

BackgroundWorker实际上是在一个单独的线程中同时执行代码.它比真正的线程更易于使用,因为它会自动与应用程序的主线程(负责控件和消息队列的一个主线程)同步(至少在我看来,我记得是正确的)(对于WPF和Windows,它是Dispatcher线程). Silverlight),因此从后台线程更新控件时无需使用Dispatcher.Invoke(或WinForms中的Control.Invoke),尽管可能不总是建议这样做.

BackgroundWorker on the other hand actually executes the code at the same time it is invoked, in a separate thread. It also is easier to use than true threads because it automatically synchronizes (at least I think I remember this correctly) with the main thread of an application, the one responsible for the controls and message queue (the Dispatcher thread in the case of WPF and Silverlight), so there's no need to use Dispatcher.Invoke (or Control.Invoke in WinForms) when updating controls from the background thread, although that may not be always recommended.

正如Reed所说,任务并行库是一个很好的替代选择.

As Reed said, Task Parallel Library is a great alternative option.

进一步的观察.

如上所述,Dispatcher并不是真正的多线程.它只会给人一种错觉,因为它确实会运行委托,而您在其他时间传递给它.只有在代码实际上仅处理应用程序的View方面时(即控件,页面,窗口等),我才使用Dispatcher.当然,它的主要用途实际上是触发 other 线程的操作以正确或在正确的时间更新控件(例如,只有在某些控件完全呈现/布局后才设置焦点)使用Dispatcher可以轻松完成此操作,因为在WPF中渲染并不完全是确定性的.

As I said above, the Dispatcher isn't really multithreaded; it only gives the illusion of it, because it does run delegates you pass to it at another time. I'd use the Dispatcher only when the code really only deals with the View aspect of an application - i.e. controls, pages, windows, and all that. And of course, its main use is actually triggering actions from other threads to update controls correctly or at the right time (for example, setting focus only after some control has rendered/laid-out itself completely is most easily accomplished using the Dispatcher, because in WPF rendering isn't exactly deterministic).

BackgroundWorker可以使多线程代码比通常的简单得多.这是一个易于理解的概念,最重要的是,您可以从中派生自定义工作程序,该工作程序可以是专门的类,可以异步执行单个任务,其属性可以用作参数,进度通知和取消等.我总是发现BackgroundWorker可以提供很大的帮助(除非我必须从它那里派生代码,以保持原始线程的文化来正确维护本地化:P)

BackgroundWorker can make multithreaded code a lot simpler than it normally is; it's a simple concept to grasp, and most of all (if it makes sense) you can derive custom workers from it, which can be specialized classes that perform a single task asynchronously, with properties that can function as parameters, progress notification and cancellation etc. I always found BackgroundWorker a huge help (except when I had to derive from it to keep the Culture of the original thread to maintain the localization properly :P)

最强大但也是最困难的路径是使用最低的可用级别System.Threading.Thread;.但是,很容易出错,因此不建议这样做.多线程编程是困难的,这是已知的.但是,如果您想了解所有方面,则有很多很好的信息:这篇出色的文章由我们的好伙伴Jon Skeet立刻想到(文章的最后一页也有很多非常有趣的链接).

The most powerful, but also difficult path is to use the lowest level available, System.Threading.Thread; however it's so easy to get things wrong that it's not really recommended. Multithreaded programming is hard, that's a given. However, there's plenty of good information on it if you want to understand all the aspects: this excellent article by our good fellow Jon Skeet immediately jumps to mind (the last page of the article also has a good number of very interesting links).

在.Net 4.0中,我们有一个不同的选项,任务并行库.我还没有做很多工作,但是从我看到的结果来看,它给人留下了深刻的印象(而PLINQ确实很棒).如果您有好奇心和学习的资源,那就是我的建议(毕竟, 应该花很多时间来学习).

In .Net 4.0 we have a different option, Task Parallel Library. I haven't worked with it much yet but from what I've seen it's impressive (and PLINQ is simply great). If you have the curiosity and resources to learn it, that's what I'd recommend (it shouldn't take that much to learn after all).

这篇关于WPF BackgroundWorker与分派器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆