Difference between Synchronization Context and Dispatcher

前端 未结 5 1012
孤独总比滥情好
孤独总比滥情好 2020-12-15 03:14

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

Application.Current.Dispatcher.Invoke(myAction);

But I sa

5条回答
  •  既然无缘
    2020-12-15 03:42

    The Dispatcher class is useful when you’re sure that you are calling within the context of the user interface thread and the SynchronizationContext is useful when you're not quite sure.

    If you obtain your Dispatcher class using the static Dispatcher.CurrentDispatcher method on some non-UI thread and call the BeginInvoke method, nothing will happen (No exception, no warning, nada).

    However, if you obtain your SynchronizationContext class via the static SynchronizationContext.Current method, it will return null if the thread is not a UI thread. This feather is very useful because it allows you to react to both a UI thread and a non-UI thread accordingly.

提交回复
热议问题