Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between this.Dispatcher.BeginInvoke() and Deployment.Current.Dispatcher.BeginInvoke() methods in Silverlight

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

like image 993
wizzardz Avatar asked Sep 26 '11 11:09

wizzardz


People also ask

What is dispatcher BeginInvoke?

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.

What is application current dispatcher?

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.


1 Answers

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.

like image 115
Gone Coding Avatar answered Nov 09 '22 08:11

Gone Coding