同步上下文和分派器之间的区别 [英] Difference between Synchronization Context and Dispatcher

查看:147
本文介绍了同步上下文和分派器之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Dispatcher 从外部像这样切换到UI线程

  Application.Current.Dispatcher.Invoke(myAction); 

但是我在一些论坛上看到有人建议使用 SynchronizationContext 而不是 Dispatcher

  SynchronizationContext.Current.Post (myAction,null); 

它们之间有什么区别,为什么 SynchronizationContext 。

解决方案

它们都有相似的效果,但是 SynchronizationContext 更为通用。



Application.Current.Dispatcher 引用应用程序的WPF调度程序,并使用调用会在该应用程序的主线程上执行委托。



SynchronizationContext。另一方面,当前根据当前线程返回不同的实现。在WPF应用程序的UI线程上调用时,它返回使用调度程序的 SynchronizationContext ,而在WinForms应用程序的UI线程上调用时,它返回另一个。 / p>

您可以在SynchronizationContext 继承的类/en-us/library/System.Threading.SynchronizationContext(v=vs.110).aspx rel = noreferrer>其MSDN文档: WindowsFormsSynchronizationContext DispatcherSynchronizationContext






使用 SynchronizationContext 时要注意的一件事是它返回 current 线程。如果您想使用另一个线程的同步上下文,例如UI线程,您必须首先获取其上下文并将其存储在变量中:

  public void Control_Event(对象发送者,EventArgs e)
{
var uiContext = SynchronizationContext.Current;
Task.Run(()=>
{
//做一些工作
uiContext.Post(/ *更新UI控件* /);
}
}

这不适用于 Application.Current.Dispatcher ,它总是返回应用程序的调度程序。


I am using Dispatcher to switch to UI thread from external like this

Application.Current.Dispatcher.Invoke(myAction);

But I saw on some forums people have advised to use SynchronizationContext instead of Dispatcher.

SynchronizationContext.Current.Post(myAction,null);

What is the difference between them and why SynchronizationContext should be used?.

解决方案

They both have similar effects, but SynchronizationContext is more generic.

Application.Current.Dispatcher refers to the WPF dispatcher of the application, and using Invoke on that executes the delegate on the main thread of that application.

SynchronizationContext.Current on the other hand returns different implementations of depending on the current thread. When called on the UI thread of a WPF application it returns a SynchronizationContext that uses the dispatcher, when called in on the UI thread of a WinForms application it returns a different one.

You can see the classes inheriting from SynchronizationContext in its MSDN documentation: WindowsFormsSynchronizationContext and DispatcherSynchronizationContext.


One thing to be aware about when using SynchronizationContext is that it returns the synchronization context of the current thread. If you want to use the synchronization context of another thread, e.g. the UI thread, you have to first get its context and store it in a variable:

public void Control_Event(object sender, EventArgs e)
{
    var uiContext = SynchronizationContext.Current;
    Task.Run(() => 
    {
        // do some work
        uiContext.Post(/* update UI controls*/);
    }
}

This does not apply to Application.Current.Dispatcher, which always returns the dispatcher for the application.

这篇关于同步上下文和分派器之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆