【问题标题】:Difference between this.Dispatcher.BeginInvoke() and Deployment.Current.Dispatcher.BeginInvoke() methods in SilverlightSilverlight 中 this.Dispatcher.BeginInvoke() 和 Deployment.Current.Dispatcher.BeginInvoke() 方法之间的区别
【发布时间】:2011-11-25 03:08:47
【问题描述】:

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

this.Dispatcher.BeginInvoke()Deployment.Current.Dispatcher.BeginInvoke() 函数的调用是否有区别?和

什么时候应该使用 this.Dispatcher.BeginInvoke()Deployment.Current.Dispatcher.BeginInvoke()

谢谢 亚历克斯

【问题讨论】:

    标签: silverlight silverlight-4.0 mvvm


    【解决方案1】:

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

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

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

    这两者在 Silverlight 中始终相同(在 WPF 中通常相同,除非您创建了额外的 UI 线程)。

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

    【讨论】:

    • 在什么情况下不一样?
    • @AnthonyWJones:Dispatcher.Current 检索与正在执行代码的线程关联的调度对象。所以在不同线程上调用时它们是不一样的。
    • @Will:你是说 Deployment.Current 还是 Dispatcher.Current?
    • @wizzardz:调度员。我不知道 Deployment.Current 的行为。
    • @Will:Deployment.Current.Dispatcher 始终与 Silverlight 中的 UI 线程相关联。 Silverlight 中没有Dispatcher.CurrentDependencyObject Dispatcher 属性也始终返回与 UI 线程关联的 Dispatcher。所以我试图想象一个存在差异的场景。
    猜你喜欢
    • 1970-01-01
    • 2021-07-15
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    相关资源
    最近更新 更多