snowy hedgehog
snowy hedgehog

Reputation: 672

Dispatcher Dilemma - How To Dispatch Properly?

I have read articles about Dispatcher in WPF but none was really very explaining the behaviour of Dispatcher very well. So my question to you guys is what does Dispatcher do exactly except keeping a queue of tasks and executing them by their priorities? What does the queue look alike? If I put 3 tasks in in sequence with priority "normal", and then subsequently one task with priority "send". Which will be executed first? In what order will the 3 tasks with priority normal be executed? Are there some really good tutorial about Dispatcher or some prove of concepts you guys would like to share?

Upvotes: 2

Views: 421

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564471

The Dispatcher is effectively just the message pump for WPF applications.

Unlike the traditional windows message pump, it does have a priority built in, so it acts like a priority queue instead of a traditional (first in, first out) queue.

If you dispatch a message with a priority of Send, it will get processed before other operations with other priorities, such as Normal. You can see the list of priorities, as well as their values, in the DispatcherPriority help. Higher priority messages are always processed prior to lower priority messages (which aren't already being handled).

As for your specific questions:

So my question to you guys is what does Dispatcher do exactly except keeping a queue of tasks and executing them by their priorities?

It handles windows messages, and processes them, just like the normal windows message pump in a traditional Win32 or Windows Forms application. The prioritized queue is built in order to process user messages in addition to standard windows messages, with a priority built in.

What does the queue look alike?

It's effectively a priority queue.

If I put 3 tasks in in sequence with priority "normal", and then subsequently one task with priority "send". Which will be executed first?

It depends. If there is other work happening at the time, the Send task will process first. If no other work is occurring, the Normal task may get executed before you submit the Send task, in which case the order will change. The Send task will get executed as soon as possible, however.

In what order will the 3 tasks with priority normal be executed?

These will get executed in the same order in which they are dispatched.

Upvotes: 3

Related Questions