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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:51:09+00:00 2026-05-24T16:51:09+00:00

I’ve got a MVVM setup. My model periodically calls some service and then invokes

  • 0

I’ve got a MVVM setup.

My model periodically calls some service and then invokes an action on the ViewModel which then updates some variables exposed to the View.

The variable is an ReadOnlyObservableCollection<T>, which has an ObservableCollection<T> it listens on.

The problem is that the Model calls the callback from a different thread, and thus it doesn’t allow me to clear the ObservableCollection<T> on a different thread.

So I thought: use the dispatcher, if we aren’t on the correct thread, invoke it:

    private void OnNewItems(IEnumerable<Slot> newItems)
    {
        if(!Dispatcher.CurrentDispatcher.CheckAccess())
        {
            Dispatcher.CurrentDispatcher.Invoke(new Action(() => this.OnNewItems(newItems)));
            return;
        }

        this._internalQueue.Clear();
        foreach (Slot newItem in newItems)
        {
            this._internalQueue.Add(newItem);
        }
    }

Code is pretty straightforward I think.

The problem is that, even though I execute it on the correct thread (I think) it still throws me an exception on the .Clear();

Why is this occuring? How can I work around it without creating my custom ObservableCollection<T>?

  • 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-24T16:51:10+00:00Added an answer on May 24, 2026 at 4:51 pm

    I typically initialize the dispatcher used by my view models in a common view model base to help ensure it is the UI thread dispatcher, as Will mentions.

    #region ViewModelBase()
    /// <summary>
    /// Initializes a new instance of the <see cref="ViewModelBase"/> class.
    /// </summary>
    protected ViewModelBase()
    {
        _dispatcher = Dispatcher.CurrentDispatcher;
    }
    #endregion
    
    #region Dispatcher
    /// <summary>
    /// Gets the dispatcher used by this view model to execute actions on the thread it is associated with.
    /// </summary>
    /// <value>
    /// The <see cref="System.Windows.Threading.Dispatcher"/> used by this view model to 
    /// execute actions on the thread it is associated with. 
    /// The default value is the <see cref="System.Windows.Threading.Dispatcher.CurrentDispatcher"/>.
    /// </value>
    protected Dispatcher Dispatcher
    {
        get
        {
            return _dispatcher;
        }
    }
    private readonly Dispatcher _dispatcher;
    #endregion
    
    #region Execute(Action action)
    /// <summary>
    /// Executes the specified <paramref name="action"/> synchronously on the thread 
    /// the <see cref="ViewModelBase"/> is associated with.
    /// </summary>
    /// <param name="action">The <see cref="Action"/> to execute.</param>
    protected void Execute(Action action)
    {
        if (this.Dispatcher.CheckAccess())
        {
            action.Invoke();
        }
        else
        {
            this.Dispatcher.Invoke(DispatcherPriority.DataBind, action);
        }
    }
    #endregion
    

    You could then invoke an action on the view model dispatcher like this:

    this.Execute(
        () =>
        {
            this.OnNewItems(newItems);
        }
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

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.