在C#WPF项目中,我有一个后台线程需要刷新一些UserControls。 为此,我使用了Dispatcher.Invoke:
Dispatcher.Invoke(DispatcherPriority.Normal, (MethodInvoker)delegate()
{
// Code
}
但我想知道:
非常感谢, Emanuele的
答案 0 :(得分:2)
何时代理中的代码将在主线程上执行?
如果所有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 :(得分:0)
SynchronizationContext.Send
调用时切换执行时。yield
或await
),然后运行您的函数。< / LI>