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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:17:05+00:00 2026-05-26T19:17:05+00:00

I’ve got code in a ViewModel that calls a service through a task. When

  • 0

I’ve got code in a ViewModel that calls a service through a task.
When the task finishes, it’ll populate an ObservableCollection.
The problem is that it’s waiting on the task to finish by using the ContinueWith method and providing TaskScheduler.FromCurrentSynchronizationContext as task scheduler, so that the OC gets updated on the UI thread.

So far so good, but when it comes to unit testing, it throws an exception saying that “the current SynchronizationContext may not be used as a TaskScheduler.”
If I use a mock SynchronizationContext on the unit test, then the ObservableCollection throws an error because it’s being updated based from the dispatcher thread.

Is there any way to work around this?

Thanks.

  • 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-26T19:17:06+00:00Added an answer on May 26, 2026 at 7:17 pm

    It’s not exactly easy, but it’s not really that hard either. What you need to do is a spin up a worker Thread that’s setup as STA and you start up the Dispatcher runtime on it. Once you have that worker sitting there you can dispatch work to it from the unit test threads which are, obviously, not initialized for this kind of work. So, first, here’s how you startup the dispatcher thread in your test setup:

    this.dispatcherThread = new Thread(() =>
    {
       // This is here just to force the dispatcher infrastructure to be setup on this thread
       Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
       {
           Trace.WriteLine("Dispatcher worker thread started.");
       }));
    
       // Run the dispatcher so it starts processing the message loop
       Dispatcher.Run();
    });
    
    this.dispatcherThread.SetApartmentState(ApartmentState.STA);
    this.dispatcherThread.IsBackground = true;
    this.dispatcherThread.Start();
    

    Now, if you want to cleanly shut that thread down in your test cleanup, which I recommend you do, you simply do the following:

    Dispatcher.FromThread(this.dispatcherThread).InvokeShutdown();
    

    So, all that infrastructure stuff out of the way, here’s all you need to do in your test to execute on that thread.

    public void MyTestMethod
    {
        // Kick the test off on the dispatcher worker thread synchronously which will block until the work is competed
        Dispatcher.FromThread(this.dispatcherThread).Invoke(new Action(() =>
        {
            // FromCurrentSynchronizationContext will now resolve to the dispatcher thread here
        }));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT

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.