Silverlight中的.Dispatcher.BeginInvoke()和Deployment.Current.Dispatcher.BeginInvoke()方法之间的区别

Silverlight中的.Dispatcher.BeginInvoke()和Deployment.Current.Dispatcher.BeginInvoke()方法之间的区别,silverlight,silverlight-4.0,mvvm,Silverlight,Silverlight 4.0,Mvvm,我知道Dispatcher.BeginInvoke()用于在UI线程中执行某些代码。最近我注意到,有另一种方法可以通过使用“Deployment”类来获取Dispatcher实例。我想知道 调用this.Dispatcher.BeginInvoke()和部署.Current.Dispatcher.BeginInvoke()函数之间是否存在差异 何时应该使用this.Dispatcher.BeginInvoke()和部署.Current.Dispatcher.BeginInvoke() 谢谢 Al

我知道Dispatcher.BeginInvoke()用于在UI线程中执行某些代码。最近我注意到,有另一种方法可以通过使用“Deployment”类来获取Dispatcher实例。我想知道

调用this.Dispatcher.BeginInvoke()部署.Current.Dispatcher.BeginInvoke()函数之间是否存在差异

何时应该使用this.Dispatcher.BeginInvoke()部署.Current.Dispatcher.BeginInvoke()

谢谢
Alex

简短回答:在Silverlight中它们是相同的,因此请使用较短的一个(如果在代码上下文中可用)。

this.Dispatcher.BeginInvoke()
确保它在相关控件运行的线程上运行

Deployment.Current.Dispatcher.BeginInvoke()
确保它在主UI线程上运行

这两个线程在Silverlight中总是相同的(在WPF中通常是相同的,除非您创建了额外的UI线程)


使用
this.Dispatcher.BeginInvoke()
除非当前上下文没有Dispatcher,否则请使用全局Dispatcher。

在什么情况下它们不一样?@AnthonyWJones:Dispatcher.current检索与执行代码的线程关联的dispatch对象。因此,在不同的线程上调用时,它们是不同的。@威尔:你是指Deployment.Current还是Dispatcher.Current?@wizzardz:Dispatcher。我不知道Deployment.Current的行为。@威尔:
Deployment.Current.Dispatcher
总是与Silverlight中的UI线程相关联。Silverlight中没有
调度程序。当前
DependencyObject
Dispatcher
属性也始终返回与UI线程关联的
Dispatcher
。所以我试着想象一个不同的场景。