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 4044504
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:15:51+00:00 2026-05-20T13:15:51+00:00

I’ve read many thread posts, articles etc. about binding and the thread affinity of

  • 0

I’ve read many thread posts, articles etc. about binding and the thread affinity of GUI controls. There are some post in which the people don’t want to use the Dispatcher.

I also have a workmate who avoids using the Dispatcher in his code. I asked him for the reason but his answer didn’t satisfy me. He said, he don’t like such kind of “magic” hidden in a class.

Well I’m a fan of the following class.


public class BindingBase : INotifyPropertyChanged
{
   public event PropertyChangedEventHandler PropertyChanged;

   private Dispatcher Dispatcher
   {
#if SILVERLIGHT
      get { return Deployment.Current.Dispatcher; }
#else
      get { return Application.Current.Dispatcher; }
#endif
   }

   protected void RaisePropertyChanged<T>(Expression<Func<T>> expr)
   {
      var memberExpr = (MemberExpression)expr.Body;
      string property = memberExpr.Member.Name;

      var propertyChanged = PropertyChanged;
      if (propertyChanged == null) return;

      if (Dispatcher.CheckAccess())
         propertyChanged.Invoke(this, new PropertyChangedEventArgs(property));
      else
         Dispatcher.BeginInvoke(() => RaisePropertyChanged(expr));
   }
}

Here is the question. Are there any reasons why some people don’t want to use such a class? Perhaps I have to reconsider this approach.

You have to admit, there is one strange thing. Dispatcher.CheckAccess() is excluded from Intellisense. Perhaps they are a bit scary due to this fact.

Regards

EDIT:

Ok, another example. Consider a complex object. The collection as example was perhaps not the best idea.


public class ExampleVm : BindingBase
{
   private BigFatObject _someData;
   public BigFatObject SomeData
   {
      get { return _someData; }
      set
      {
         _someData = value;
         RaisePropertyChanged(() => SomeData);
      }
   }

   public ExampleVm()
   {
      new Action(LoadSomeData).BeginInvoke(null, null); //I know - it's quick and dirty
   }

   private void LoadSomeData()
   {
      // loading some data from somewhere ...
      // result is of type BigFatObject

      SomeData = result; // This would not work without the Dispatcher, would it?
   }
}
  • 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-20T13:15:51+00:00Added an answer on May 20, 2026 at 1:15 pm

    I am also, personally, not against of Dispatcher in view model classes. I haven’t seen any significant problems with it, yet it gives the most flexibility to your code.

    But I like the idea of encapsulating the usage of Dispatcher in the infrastructure code as much as possible. Just like you did with RaisePropertyChanged method (BTW, in case of RaisePropertyChanged you don’t have to dispatch anything – binding already does it for you; you only have to dispatch changes to collections).

    The biggest and the only disadvantage that I see here is unit testing. Things can get tricky when you try to test your logic that involves usage of Dispatcher. Imagine if you had code something like this in a view model:

    private void UpdateMyCollection() 
    {
       IList<ModelData> dataItems = DataService.GetItems();
    
       // Update data on UI
       Dispatcher.BeginInvoke(new Action(() => {
          foreach (ModelData dataItem in dataItems)
          {
             MyObservableCollection.Add(new DataItemViewModel(dataItem));
          }
       }));
    }
    

    This kind of code is quite typical when it comes to updating collections from a non-UI thread. Now, how would you write a unit test that tests the logic of adding items to the observable collection? First of all, you will need to mock the Dispatcher property because Application.Current is null during unit test execution. Second of all, how will you mock it? Will you create a special thread that will mimic a UI thread and use the Dispatcher of that thread? So, this kind of things.

    The bottom line is that if you want your code to be unit-test-friendly, you need to think of the way how you will mock the Dispatcher. This is the only concern.

    Update:

    The second example you provided WILL work without Dispatcher (binding will do the trick).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6
I'm looking for suggestions for debugging... If you view this site in Firefox or
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

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.