Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of Deployment.Current.Dispatcher.BeginInvoke( ()=> {...} )?

I have seen this Deployment.Current.Dispatcher.BeginInvoke( ()=> {...} ) format in some code .Is it used to do some work in Background?What are the general uses of it?

like image 932
Vaysage Avatar asked Jan 13 '11 11:01

Vaysage


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 the use of dispatcher in C#?

The Dispatcher maintains a prioritized queue of work items for a specific thread. When a Dispatcher is created on a thread, it becomes the only Dispatcher that can be associated with the thread, even if the Dispatcher is shut down.

What is application current dispatcher?

CurrentDispatcher may create a new instance of Dispatcher depending on the thread from which it was called. 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.


2 Answers

No, it's not to do work in a background thread - it's to do work on the UI thread. So it's normally called from a background thread, in order to manipulate the UI, which can only be done on the UI thread.

The body of the lambda expression is the code which you want to execute in the UI thread.

like image 61
Jon Skeet Avatar answered Oct 07 '22 00:10

Jon Skeet


When code that updates the UI executes from a thread other than the UI thread, an invalid cross-thread access exception occurs.

The dispatcher allows you to pass some code over to the UI thread from another thread.

The project I put in this post demonstrates this, among other concepts.

WebClient, HttpWebRequest and the UI Thread on Windows Phone 7

like image 27
Mick N Avatar answered Oct 06 '22 22:10

Mick N