I know Dispatcher.BeginInvoke() is used to execute some piece of code in the UI thread.Recently I noticed that there is another way to get the Dispatcher instance by using 'Deployment' class. I would like to know
Is there any diffrence between the invokation of this.Dispatcher.BeginInvoke() and Deployment.Current.Dispatcher.BeginInvoke() functions ?, and
when should I use this.Dispatcher.BeginInvoke() and Deployment.Current.Dispatcher.BeginInvoke() ?
Thanks Alex
BeginInvoke(DispatcherPriority, Delegate, Object) Executes the specified delegate asynchronously at the specified priority and with the specified argument on the thread the Dispatcher is associated with.
That is correct, the Application. Current. Dispatcher is an instance property of the application which is assigned upon construction to be the dispatcher of the current thread. And as the documentation of Dispatcher.
Short answer: They are the same in Silverlight, so use the shorter one (if available in the context of your code).
this.Dispatcher.BeginInvoke()
ensures it is run on the thread that the control in question is running under.
Deployment.Current.Dispatcher.BeginInvoke()
ensures it is run on the main UI thread.
The two are always the same in Silverlight (and usually the same in WPF, unless you have created extra UI threads).
Use this.Dispatcher.BeginInvoke()
unless your current context does not have a dispatcher, then use the global one instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With