Application Dispatcher and Control Dispatcher

后端 未结 3 1625
野趣味
野趣味 2021-01-14 18:13

Assume i have control button with name \"button1\" and function with name \"doSomething\". Function \"doSomething\" is called from another thread.

i have two method

3条回答
  •  一整个雨季
    2021-01-14 18:49

    In most of the case, We have single UI thread. So, It doesn't make difference you call

    control.Dispatcher(which comes inherited from DispatcherObject parent of the controls).
    
    or 
    
    Disptacher.Current.
    

    But there are scenarios, where you will end up having multiple dispatchers. So, in that situation, Control.Dispatcher will help as It will find out the current dispatcher to respect Thread Affinity. In this Dispatcher.Current won't help.

    One scenario, having dedicated thread(with dispatcher) for displaying busy indicator as default UI thread is busy in rendering large list of controls.

    However, with SynchronizationContext is another way to avoid such issues. but what if that context or thread is no longer required or it has been set to null by any other developer. So, It is always wise to go for Control.Dispatcher in my opinion.

提交回复
热议问题