繁体   English   中英

何时执行Dispatcher.Invoke中的代码?

[英]When the code inside Dispatcher.Invoke is executed?

在C#WPF项目中,我有一个后台线程需要刷新一些UserControls。 为此,我使用了Dispatcher.Invoke:

    Dispatcher.Invoke(DispatcherPriority.Normal, (MethodInvoker)delegate()
    {
        // Code
    }

但我想知道:

  • 当代理中的代码将在主线程上执行时?
  • 要执行它,可以停止正常运行在主线程上的方法吗?
  • 是否可以使用Dispatcher.Invoke导致竞争条件?

非常感谢,Emanuele

当代理中的代码将在主线程上执行时?

如果operations with priority greater than Normal got a chance to execute所有operations with priority greater than Normal got a chance to execute则委托将在主线程上执行一次。

要执行它,可以停止正常运行在主线程上的方法吗?

Invoke方法使委托在主线程上同步执行,但如果要异步执行它,则需要使用BeginInvoke。 如果某个委托在主线程上运行,您的delegate will get queued and will run once executing operation gets completed 但是,除非委托执行,否则后台线程不会继续前进。

是否可以使用Dispatcher.Invoke导致竞争条件?

No ,它不会因为委托在Dispatcher Queue中排队。

  1. 确切地说,当UI线程能够在SynchronizationContext.Send调用上切换其执行时。
  2. 执行它时,通常在主线程上运行的方法将完成运行(包括其他伪非完成条件,如yieldawait ),然后运行您的函数。
  3. 没有。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM