[英]When the code inside Dispatcher.Invoke is executed?
在C#WPF项目中,我有一个后台线程需要刷新一些UserControls。 为此,我使用了Dispatcher.Invoke:
Dispatcher.Invoke(DispatcherPriority.Normal, (MethodInvoker)delegate()
{
// Code
}
但我想知道:
非常感谢,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中排队。
SynchronizationContext.Send
调用上切换其执行时。 yield
或await
),然后运行您的函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.