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

  • Home
  • SEARCH
  • 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 9083705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:50:11+00:00 2026-06-16T20:50:11+00:00

I have Visual Studio 2012 and an asynchronous test that needs a synchronization context.

  • 0

I have Visual Studio 2012 and an asynchronous test that needs a synchronization context.
But the default synchronization context of MSTest is null.
I would like to test as running on a WPF- or WinForms-UI thread that has a synchronization context.
What’s the best method to add a SynchronizationContext to the test thread ?

    [TestMethod]
    public async Task MyTest()
    {
        Assert.IsNotNull( SynchronizationContext.Current );
        await MyTestAsync();
        DoSomethingOnTheSameThread();
    }
  • 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-06-16T20:50:12+00:00Added an answer on June 16, 2026 at 8:50 pm

    Using the informations from Panagiotis Kanavos and Stephen Cleary, I can write my testmethods like this:

        [TestMethod]
        public void MyTest()
        {
          Helper.RunInWpfSyncContext( async () =>
          {
            Assert.IsNotNull( SynchronizationContext.Current );
            await MyTestAsync();
            DoSomethingOnTheSameThread();
          });
        }
    

    The inner code now runs in a WPF synchronization context and handles all exceptions as used for MSTest.
    The Helper method is from Stephen Toub:

    using System.Windows.Threading; // WPF Dispatcher from assembly 'WindowsBase'
    
    public static void RunInWpfSyncContext( Func<Task> function )
    {
      if (function == null) throw new ArgumentNullException("function");
      var prevCtx = SynchronizationContext.Current;
      try
      {
        var syncCtx = new DispatcherSynchronizationContext();
        SynchronizationContext.SetSynchronizationContext(syncCtx);
    
        var task = function();
        if (task == null) throw new InvalidOperationException();
    
        var frame = new DispatcherFrame();
        var t2 = task.ContinueWith(x=>{frame.Continue = false;}, TaskScheduler.Default);
        Dispatcher.PushFrame(frame);   // execute all tasks until frame.Continue == false
    
        task.GetAwaiter().GetResult(); // rethrow exception when task has failed 
      }
      finally
      { 
        SynchronizationContext.SetSynchronizationContext(prevCtx);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have installed Visual Studio 2012 RC. I would like to find any example
I have installed the new Visual Studio 2012 Ultimate. I have created a Test
I would love to try f# 3.0 but I only have visual studio 2010
I have a Visual Studio 2012 C# app that uses a WinForms UserControl nested
I have a Visual Studio 2012 solution that consists of both a web application
I have downloaded Visual Studio Express 2012 for Windows 8. I think that to
Visual Studio 2012, TFS, Windows 7 We have a C# solution that has 4
I have a Visual Studio 2012 web project that I am publishing to a
I have a WCF SOAP consumer that is implemented by Visual Studio 2012 from
Started using Visual Studio 2012 RC since yesterday, We have one WCF solution. Whenever

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.