Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DispatcherPriority in WPF

Tags:

c#

wpf

Is there any documentation as to the exact meanings of each of the DispatcherPriority enumeration values, in relation to a WPF application.

In particular, is there any practical difference between Background / ContextIdle / ApplicationIdle ? I've found myself using each of these in various parts of my code and would like to standardise on one value.

A typical usage would be in a data bound TreeView control. When setting an item as selected (in the ViewModel), I want to first expand all of its parent items (again in the ViewModel), then wait for them to be fully rendered (in the View) before actually setting the selected property.

like image 769
Peregrine Avatar asked Dec 21 '17 11:12

Peregrine


People also ask

What is dispatcher WPF?

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.

What is dispatcher invoke in C#?

Invoke(Action, DispatcherPriority) Executes the specified Action synchronously at the specified priority on the thread the Dispatcher is associated with. Invoke(Action) Executes the specified Action synchronously on the thread the Dispatcher is associated with.

What is dispatcher BeginInvoke?

BeginInvoke(DispatcherPriority, Delegate) Executes the specified delegate asynchronously at the specified priority on the thread the Dispatcher is associated with. BeginInvoke(Delegate, DispatcherPriority, Object[])

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.


1 Answers

Best way to understand how it is actually working is to see source code of it.

.Net Framework source code is available at https://referencesource.microsoft.com/

You can get answers for your question after checking out and understanding this code https://referencesource.microsoft.com/#WindowsBase/Base/System/Windows/Threading/Dispatcher.cs,ad208569500b2a1d

My quick understanding: there is a lot of priorities is just to give oportunity to specify priority of operation more precise if it will be needed by your code. There is no hidden events/states to perform operations of specific priorities. Alghoritm will take task with top priority and invoke it, and so on.

like image 119
Mikolaytis Avatar answered Sep 18 '22 12:09

Mikolaytis