Click here to Skip to main content
Click here to Skip to main content
Technical Blog

WPF Inside Out: Dispatcher

, 15 Sep 2010 Ms-PL
Rate this:
Please Sign up or sign in to vote.
In this post, we’ll see a few interesting things about WPF’s Dispatcher class.

Back in this post, I showed you how you can have a look at the original source code of .NET, including original comments and variable names.

In this post, we'll see a few interesting things about WPF’s Dispatcher class. But first, some background on the subject.

WPF Thread Affinity

Almost every WPF element has thread affinity. This means that access to such an element should be made only from the thread that created the element. In order to do so, every element that requires thread affinity is derived, eventually, from DispatcherObject class. This class provides a property named Dispatcher that returns the Dispatcher object associated with the WPF element.

The Dispatcher Class

The Dispatcher class is used to perform work on his attached thread. It has a queue of work items and it is in charge of executing the work items on the dispatcher thread.

So, when you want to change a property of a WPF element from a thread different from the one who created the element, you should use the element’s Dispatcher property to dispatch the operation to the correct thread. This is done using the BeginInvoke method that accepts a method to be invoked.

Where is the Dispatcher of the Thread Saved?

Every thread should have their dispatcher object, so you would think they will save the dispatcher on the Thread Local Storage (TLS).

It turns out they store a static list of all available dispatcher objects.
Of course, this list is synchronized using a private global static object (this is a common best practice when locking object).

Whenever the dispatcher of an object is needed, they go over the list, comparing the dispatcher’s Thread property with the current thread, until they find the correct dispatcher object for this thread.

The reason, as they note in the comments is that managed TLS is rather expensive.

Above this list, they add the following optimization: before going over the dispatchers list, they check if the last given dispatcher is suitable, so only a thread context switch will derive a new list search.

Although strange, this usually isn't such a problem because most applications will only have one thread that creates UI elements, hence only one Dispatcher in the list.

That’s it for now,
Arik Poznanski

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Arik Poznanski
Software Developer (Senior) Verint
Israel Israel
Arik Poznanski is a senior software developer at Verint. He completed two B.Sc. degrees in Mathematics & Computer Science, summa cum laude, from the Technion in Israel.
 
Arik has extensive knowledge and experience in many Microsoft technologies, including .NET with C#, WPF, Silverlight, WinForms, Interop, COM/ATL programming, C++ Win32 programming and reverse engineering (assembly, IL).
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralMy vote of 5 PinmemberGary Wheeler21-Sep-10 1:11 
GeneralIt isn't intermediate, but... PinmemberYury Goltsman20-Sep-10 23:25 
GeneralRe: It isn't intermediate, but... PinmemberGary Wheeler21-Sep-10 1:15 
GeneralRe: It isn't intermediate, but... PinmemberYury Goltsman21-Sep-10 5:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 16 Sep 2010
Article Copyright 2010 by Arik Poznanski
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid