.net Dispatcher.CurrentDispatcher 与 Application.Current.Dispatcher
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10448987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dispatcher.CurrentDispatcher vs. Application.Current.Dispatcher
提问by ken
What are the differences between Dispatcher.CurrentDispatcher(in System.Windows.Threading) and Application.Current.Dispatcher(in System.Windows)?
Dispatcher.CurrentDispatcher(in System.Windows.Threading) 和Application.Current.Dispatcher(in System.Windows)之间有什么区别?
My gut tells me that Application.Current.Dispatcherwill never change and is global to all threads in the current application, while Dispatcher.CurrentDispatchermay create a new instance of Dispatcherdepending on the thread from which it was called.
我的直觉告诉我,它Application.Current.Dispatcher永远不会改变,并且对当前应用程序中的所有线程都是全局的,同时Dispatcher.CurrentDispatcher可能会Dispatcher根据调用它的线程创建一个新实例。
Is that correct?
那是对的吗?
If it is, is the purpose of Dispatcher.CurrentDispatcherprimarily for multi-threaded UI?
如果是,Dispatcher.CurrentDispatcher主要是为了多线程 UI的目的吗?
回答by Jon
My gut tells me that Application.Current.Dispatcher will never change and is global to all threads in the current application, while Dispatcher.CurrentDispatcher may create a new instance of Dispatcher depending on the thread from which it was called.
我的直觉告诉我 Application.Current.Dispatcher 永远不会改变并且对当前应用程序中的所有线程都是全局的,而 Dispatcher.CurrentDispatcher 可能会根据调用它的线程创建一个新的 Dispatcher 实例。
That is correct.
那是正确的。
Additionally, there is no point whatsoever in accessing Dispatcher.CurrentDispatcherfrom a non-UI thread. It will do nothing unless you call Dispatcher.Run, and going into an infinite message loop is not what you want to be doing from within worker threads.
此外,Dispatcher.CurrentDispatcher从非 UI 线程访问毫无意义。除非您调用Dispatcher.Run,否则它将什么也不做,并且进入无限消息循环并不是您想要在工作线程中执行的操作。
So:
所以:
In the most common scenario, where your app only has a single UI thread,
Application.Current.DispatcherandDispatcher.CurrentDispatcherfrom within the UI thread will return the same instance. Which one you use is simply a matter of preference.If your app has more than one UI thread then each
DispatcherObjectwill be permanently associated with the dispatcher of the UI thread it was created in upon construction. In this case,Application.Current.Dispatcherwill refer to the dispatcher of the thread your application spawned with; you will not be able to use it to post messages to controls owned by your other UI threads.
在最常见的情况下,您的应用程序只有一个 UI 线程,
Application.Current.Dispatcher并且Dispatcher.CurrentDispatcher从 UI 线程中返回相同的实例。您使用哪一个只是一个偏好问题。如果您的应用程序有多个 UI 线程,那么每个线程都
DispatcherObject将与在构建时创建的 UI 线程的调度程序永久关联。在这种情况下,Application.Current.Dispatcher将指代您的应用程序生成的线程的调度程序;您将无法使用它向其他 UI 线程拥有的控件发布消息。
回答by ken
To put it simply...
简而言之...
Dispatcher.CurrentDispatchergets the dispatcher for the current thread. So, if you're looking for the UI thread's Dispatcher from a background process, don't use this.
Dispatcher.CurrentDispatcher获取当前线程的调度程序。因此,如果您正在从后台进程中寻找 UI 线程的 Dispatcher,请不要使用它。
Application.Current.Dispatcherwill always give you the UI thread's dispatcher, as this is the thread that spins up the sole Application instance.
Application.Current.Dispatcher将始终为您提供 UI 线程的调度程序,因为这是启动唯一应用程序实例的线程。
回答by H.B.
My gut tells me that Application.Current.Dispatcher will never change and is global to all threads in the current application, while Dispatcher.CurrentDispatcher may create a new instance of Dispatcher depending on the thread from which it was called.
我的直觉告诉我 Application.Current.Dispatcher 永远不会改变并且对当前应用程序中的所有线程都是全局的,而 Dispatcher.CurrentDispatcher 可能会根据调用它的线程创建一个新的 Dispatcher 实例。
That is correct, the Application.Current.Dispatcheris an instance property of the application which is assigned upon construction to be the dispatcher of the current thread. And as the documentation of Dispatcher.CurrentDispatcherpoints out:
这是正确的,Application.Current.Dispatcher是应用程序的一个实例属性,它在构造时被分配为当前线程的调度程序。正如文档Dispatcher.CurrentDispatcher指出的那样:
Gets the Dispatcher for the thread currently executing and creates a new Dispatcher if one is not already associated with the thread.
获取当前正在执行的线程的 Dispatcher,如果一个 Dispatcher 尚未与线程关联,则创建一个新的 Dispatcher。
If it is, is the purpose of Dispatcher.CurrentDispatcher primarily for multi-threaded UI?
如果是,那么 Dispatcher.CurrentDispatcher 的目的是否主要用于多线程 UI?
Possibly, i have not encountered any use in getting the dispatcher of a background thread as you usually have no UI-elments belonging to them to which you may want to dispatch operations.
可能,我在获取后台线程的调度程序方面没有遇到任何用处,因为您通常没有属于它们的 UI 元素,您可能希望向其调度操作。

