It wasn’t until I’d used WPF for about a year that I really started to appreciate the value of using the Dispatcher to execute chunks of code asynchronously with a specific DispatcherPriority. Now I would say I use this a couple of times a day and find it invaluable.
In this series of posts, I’m going to introduce the Dispatcher object and DispatcherPriority enum, and outline some of the useful ways to leverage them.
In this first post I’ll introduce the Dispatcher and DispatcherPriority, and then I’ll follow it up with posts showing some uses for them:
- Execute a Method Asynchronously Using the Dispatcher Queue
- Load the Data for a Window Asynchronously After It Has Rendered
- Load the Items in a ListBox Asynchronously
- Implement Application.DoEvents in WPF
- BackgroundCommand (and demo)
- Silverlight – combo box
- Dual Targeting – DispatcherHelper
Introducing the Dispatcher and DispatcherPriority
If you need to execute a method asynchronously without blocking the UI thread, you can invoke the method on the Dispatcher property of a UI element, and specify a System.Windows.Threading.DispatcherPriority that’s lower than the Render priority.
Read the rest of this entry »