Cross Thread User Interface updating in WPF

Standard

Recently I tried to update a WPF Dependency property from a System.Timer thread and received a “Cross Threading” error.  This wasn’t entirely unexpected, as I’d encountered similar issues in Winforms in the past.

In Winforms, methods such as Invoke, BeginInvoke and InvokeRequired are used in order to force code to run on the UI thread.

The absence of these methods (but not the error message) in WPF was disconcerting.  Fortunately those methods (and many more) do exist, via an extra object called the “Dispatcher” object that hangs off all Ui Elements.  

image

Links

Forum Post – http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=328693&SiteID=1

WPF Threads: Build More Responsive Apps With The Dispatcher — MSDN Magazine, October 2007

Advertisements

One response »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s