Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 720301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:44:33+00:00 2026-05-14T05:44:33+00:00

I had a Invalid Cross Thread access issue, but a little research and I

  • 0

I had a Invalid Cross Thread access issue, but a little research and I managed to fix it by using the Dispatcher.

Now in my app I have objects with lazy loading. I’d make an Async call using WCF and as usual I use the Dispatcher to update my objects DataContext, however it didn’t work for this scenario. I did however find a solution here. Here’s what I don’t understand.

In my UserControl I have code to call an Toggle method on my object. The call to this method is within a Dispatcher like so.

Dispatcher.BeginInvoke( () => _CurrentPin.ToggleInfoPanel() );

As I mentioned before this was not enough to satisfy Silverlight. I had to make another Dispatcher call within my object. My object is NOT a UIElement, but a simple class that handles all its own loading/saving.

So the problem was fixed by calling

Deployment.Current.Dispatcher.BeginInvoke( () => dataContext.Detail = detail );

within my class.

Why did I have to call the Dispatcher twice to achieve this? Shouldn’t a high-level call be enough? Is there a difference between the Deployment.Current.Dispatcher and the Dispatcher in a UIElement?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:44:34+00:00Added an answer on May 14, 2026 at 5:44 am

    Ideally, store a single instance of Dispatcher that you can use elsewhere without having the thread check on it.

    Calling any singleton .Current instance may in fact cause a cross-thread access check to be invoked. By storing it first, you can avoid this to actually get the shared instance.

    I use a “SmartDispatcher” that uses a dispatcher when called off-thread, and just invokes otherwise. It solves this sort of issue.

    Post: http://www.jeff.wilcox.name/2010/04/propertychangedbase-crossthread/

    Code:

    // (c) Copyright Microsoft Corporation.
    // This source is subject to the Microsoft Public License (Ms-PL).
    // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
    // All other rights reserved.
    
    using System.ComponentModel;
    
    namespace System.Windows.Threading
    {
        /// <summary>
        /// A smart dispatcher system for routing actions to the user interface
        /// thread.
        /// </summary>
        public static class SmartDispatcher
        {
            /// <summary>
            /// A single Dispatcher instance to marshall actions to the user
            /// interface thread.
            /// </summary>
            private static Dispatcher _instance;
    
            /// <summary>
            /// Backing field for a value indicating whether this is a design-time
            /// environment.
            /// </summary>
            private static bool? _designer;
    
            /// <summary>
            /// Requires an instance and attempts to find a Dispatcher if one has
            /// not yet been set.
            /// </summary>
            private static void RequireInstance()
            {
                if (_designer == null)
                {
                    _designer = DesignerProperties.IsInDesignTool;
                }
    
                // Design-time is more of a no-op, won't be able to resolve the
                // dispatcher if it isn't already set in these situations.
                if (_designer == true)
                {
                    return;
                }
    
                // Attempt to use the RootVisual of the plugin to retrieve a
                // dispatcher instance. This call will only succeed if the current
                // thread is the UI thread.
                try
                {
                    _instance = Application.Current.RootVisual.Dispatcher;
                }
                catch (Exception e)
                {
                    throw new InvalidOperationException("The first time SmartDispatcher is used must be from a user interface thread. Consider having the application call Initialize, with or without an instance.", e);
                }
    
                if (_instance == null)
                {
                    throw new InvalidOperationException("Unable to find a suitable Dispatcher instance.");
                }
            }
    
            /// <summary>
            /// Initializes the SmartDispatcher system, attempting to use the
            /// RootVisual of the plugin to retrieve a Dispatcher instance.
            /// </summary>
            public static void Initialize()
            {
                if (_instance == null)
                {
                    RequireInstance();
                }
            }
    
            /// <summary>
            /// Initializes the SmartDispatcher system with the dispatcher
            /// instance.
            /// </summary>
            /// <param name="dispatcher">The dispatcher instance.</param>
            public static void Initialize(Dispatcher dispatcher)
            {
                if (dispatcher == null)
                {
                    throw new ArgumentNullException("dispatcher");
                }
    
                _instance = dispatcher;
    
                if (_designer == null)
                {
                    _designer = DesignerProperties.IsInDesignTool;
                }
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public static bool CheckAccess()
            {
                if (_instance == null)
                {
                    RequireInstance();
                }
    
                return _instance.CheckAccess();
            }
    
            /// <summary>
            /// Executes the specified delegate asynchronously on the user interface
            /// thread. If the current thread is the user interface thread, the
            /// dispatcher if not used and the operation happens immediately.
            /// </summary>
            /// <param name="a">A delegate to a method that takes no arguments and 
            /// does not return a value, which is either pushed onto the Dispatcher 
            /// event queue or immediately run, depending on the current thread.</param>
            public static void BeginInvoke(Action a)
            {
                if (_instance == null)
                {
                    RequireInstance();
                }
    
                // If the current thread is the user interface thread, skip the
                // dispatcher and directly invoke the Action.
                if (_instance.CheckAccess() || _designer == true)
                {
                    a();
                }
                else
                {
                    _instance.BeginInvoke(a);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a problem with accepting invalid SSL certificate in my iPhone program. That
I had called WebSitePayment Pro doDirectPayment API with all valid inputs but in API
i get the error invalid label {ok : 1, msg : Uh, we had
Has anyone had any luck model binding two or more collections using the code
I've had this project running for a couple of months using a couple of
Had an interesting discussion with some colleagues about the best scheduling strategies for realtime
Had a coworker ask me this, and in my brain befuddled state I didn't
I had used Server Explorer and related tools for graphical database development with Microsoft
I had been happily coding along on a decent sized solution (just over 13k
I had a discussion with some colleagues mentioning that there are not too many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.