Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Dispatcher differs from the background thread?

How does the Dispatcher concept in .NET 3.5 and WPF differ from the background thread in .NET 2.0 ?

For example what will be difference between statements below:

delegate.Invoke/BeginInvoke

AND

this.dispatcher.Invoke/BeginInvoke
like image 855
Ashish Ashu Avatar asked Mar 29 '10 02:03

Ashish Ashu


People also ask

What is the relationship between threads and dispatchers?

A Dispatcher is responsible for managing the work for a thread. The UI thread is the thread that renders the UI. The UI thread queues work items inside an object called a Dispatcher. The Dispatcher selects work items on a priority basis and runs each one to completion.

What is background thread in android?

Background processing in Android refers to the execution of tasks in different threads than the Main Thread, also known as UI Thread, where views are inflated and where the user interacts with our app.

What is the use of a Dispatcher object in WPF?

This class provides a property named Dispatcher that returns the Dispatcher object associated with the WPF element. The Dispatcher class is used to perform work on its attached thread. It has a queue of work items and it is in charge of executing the work items on the dispatcher thread. Save this answer.

What is thread affinity in WPF?

WPF requires that most of its objects be tied to the UI thread. This is known as thread affinity, meaning you can only use a WPF object on the thread on which it was created. Using it on other threads will cause a runtime exception to be thrown.


2 Answers

The dispatcher can be thought of as a Queue that events are sent to; a dispatcher will run on the UI thread and execute events for the UI. In windows, UI controls may only be modified by the thread that created them, so any changes to the UI must be done from the UI thread - thus that is one of the critical reasons why operations that modify window elements must be sent to the UI's dispatcher.

A background thread, in turn, is a different thread than the UI. So anything run on one of these threads will not affect or block the UI.

like image 86
Justin Ethier Avatar answered Sep 28 '22 05:09

Justin Ethier


The concept of BeginInvoke and Invoke can be thought of as follows.

  • BeginInvoke means: "Do this and return before it completes. I either don't care about the return value or you can call me back at this address at some point in the future."
  • Invoke means: "Do this and I'll sit here and wait for it to complete."

Now how this relates to dispatchers and background threads is another matter altogether. As Justin says, the Dispatcher processes a queue of things to do every time the UI thread becomes idle. A background thread that calls BeginInvoke on the dispatcher will return immediately even though the dispatcher may not have gotten around to processing. If Invoke had been used instead, the background thread would block until the UI thread completed processing. Note that in Silverlight, there is no Invoke on the Dispatcher and in most cases you probably don't want your background thread blocking while the UI thread is processing work.

Conversely, Delegate.BeginInvoke uses worker threads in the thread pool. When you're on the UI thread (or any thread really) you can call BeginInvoke and Invoke on a delegate. BeginInvoke will use a worker thread to call the delegate using the same semantics I described above. Invoke, however, would not use a different thread. It would simply invoke the delegate synchronously in the context of the calling thread and return when completed.

Be careful when using synchronous execution across threads though as this often results in deadlocks if you're not very careful.

like image 21
Josh Avatar answered Sep 28 '22 05:09

Josh