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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:32:28+00:00 2026-05-18T12:32:28+00:00

I am looking for general thoughts and/or links on the topic in general, although

  • 0

I am looking for general thoughts and/or links on the topic in general, although my specific motivation at the moment are UI tasks related to progress reporting using either BackgroundWorker and / or TPL. My experience level with async programming in general is novice. The testing tools I know best are NUnit and Rhino.

Some brain storming ideas off the top of my head:

  1. Don’t bother – it’s too complicated and you just wind up testing the BGW or TPL.
  2. Make some sort of fake or mock.
  3. Use EventWaitHandles
  • 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-18T12:32:28+00:00Added an answer on May 18, 2026 at 12:32 pm

    Unit testing asynchronous code is not the simplest thing in the world, as I learned when writing unit tests for my Nito.Async library. 🙂

    First, you want to define what you actually want to test. Do you just want to test whether the asynchronous action is performed, or do you want to ensure that your BGW/tasks are properly synchronizing their UI progress reporting?

    Testing the action is pretty straightforward: just wait until the action is complete and then check for postconditions. (However, be aware that the BGW RunWorkerCompleted will be raised on a ThreadPool thread unless you give it a synchronization context like the example below).

    Testing proper synchronization (e.g., that each piece of code is running on the correct thread) is more complex.

    For each test you’ll need to establish a synchronization context. This will be mocking the UI synchronization context. My Nito.Async library may help with that; it has an ActionThread which is a separate thread that contains a synchronization context suitable for owning EAP components (e.g., BGW) and scheduling tasks (e.g., TaskScheduler.FromCurrentSynchronizationContext).

    It can be used like this (using an MSTest example):

    [TestMethod]
    public void Test()
    {
      using (ActionThread thread = new ActionThread())
      {
        thread.Start();
    
        // Anything passed to Do is executed in that ActionThread.
        thread.Do(() =>
        {
          // Create BGW or call TaskScheduler.FromCurrentSynchronizationContext.
        });
    
        // The thread is gracefully exited at the end of the using block.
      }
    }
    

    I find Thread.CurrentThread.ManagedThreadId and Thread.CurrentThread.IsThreadPoolThread to be the easiest ways to check for proper synchronization. If your test code is run from within ActionThread.Do, then it should synchronize its progress updates (and completion notification) to that ActionThread.

    A lot of the Nito.Async unit tests use ActionThread in this manner, so you could look there for various examples.

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

Sidebar

Related Questions

I’m looking for some samples/tutorials/general pointers in doing either of the following. 1) Create
I'm not looking so much for language-specific answers, just general models for implementing a
Looking for a general purpose WPF toolset. Currently looking at Telerik and Infragistics. Are
I'm looking for a general solution for upgrading database schema with ORM tools, like
I'm not looking for a general discussion on if math is important or not
Looking for a toolkit/SDK for general Blackberry development or application toolkits. Anything like an
In general, how expensive is locking in Java? Specifically in my case: I have
Looking for feedback on : http://code.google.com/p/google-perftools/wiki/GooglePerformanceTools
Looking for an example that: Launches an EXE Waits for the EXE to finish.
Looking for C# class which wraps calls to do the following: read and write

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.