且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

同步上下文和分派器之间的区别

更新时间:2023-01-28 08:18:45

它们都有相似的效果,但是 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 ong>线程。如果您想使用另一个线程的同步上下文,例如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.