Click here to Skip to main content
13,258,055 members (73,849 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 19 Apr 2012

Invoking Through the Dispatcher on Windows Phone and Metro

, 19 Apr 2012
Rate this:
Please Sign up or sign in to vote.
Invoking through the Dispatcher on Windows Phone and Metro

On Windows Platforms, there is a rule that you cannot modify UI elements from secondary threads. On Microsoft's XAML based platforms, there is a member that exists on most UI objects called the Dispatcher that can be used to marshal a call to the proper thread.

On Windows Phone 7 and Windows 8, the way you go about calling this thread differs. I was recently working with some code that needed to compile for both platforms and wanted to minimize the amount of code that had to be wrapped in conditional compilation blocks. To do this, I made a single method to handle my dispatching. The method itself contains conditional compilation blocks but because of this method, I didn't need the blocks when I needed to perform operations on the UI thread.

public void DispatchInvoke(Action a)
    if (Dispatcher == null)
    if ((Dispatcher != null) && (!Dispatcher.HasThreadAccess))
                    (obj, invokedArgs) => { a(); }, 

The code will compile for both Windows Phone 7 and Windows 8 Metro without any alterations. Using the code is the same regardless of which of the two platforms you are using.

    //your operations go here
    TextBox1.Text="My Text";


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Joel Ivory Johnson
Software Developer Razorfish
United States United States
I attended Southern Polytechnic State University and earned a Bachelors of Science in Computer Science and later returned to earn a Masters of Science in Software Engineering.

For the past few years I've been providing solutions to clients using Microsoft technologies for web and Windows applications.

While most of my articles are centered around Windows Phone it is only one of the areas in which I work and one of my interests. I also have interest in mobile development on Android and iPhone. Professionally I work with several Microsoft technologies including SQL Server technologies, Silverlight/WPF, ASP.Net and others. My recreational development interest are centered around Artificial Inteligence especially in the area of machine vision.


You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Leonardo Paneque19-Apr-12 12:58
memberLeonardo Paneque19-Apr-12 12:58 
GeneralMy vote of 5 Pin
Justin Cooney19-Apr-12 6:59
memberJustin Cooney19-Apr-12 6:59 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 19 Apr 2012
Article Copyright 2012 by Joel Ivory Johnson
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid