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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:13:59+00:00 2026-05-30T05:13:59+00:00

I am trying to follow the suggestions from Using the WPF Dispatcher in unit

  • 0

I am trying to follow the suggestions from Using the WPF Dispatcher in unit tests in order to get my nUnit test to run.

When I write my unit test as below, it works:

[Test]
public void Data_Should_Contain_Items()
{
    DispatcherFrame frame = new DispatcherFrame();
        PropertyChangedEventHandler waitForModelHandler = delegate(object sender, PropertyChangedEventArgs e)
        {
          if (e.PropertyName == "Data")
          {
            frame.Continue = false;
          }
        };
    _myViewModel.PropertyChanged += waitForModelHandler;
    Dispatcher.PushFrame(frame);

    Assert.IsTrue(_myViewModel.Data.Count > 0, "Data item counts do not match");
}

However, if I try to use the suggestion of the DispatcherUtil, it does not work:

[Test]
public void Data_Should_Contain_Items()
{
    DispatcherUtil.DoEvents();
    Assert.IsTrue(_myViewModel.Data.Count > 0, "Data item counts do not match");
}

public static class DispatcherUtil
{
    [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
    public static void DoEvents()
    {
        DispatcherFrame frame = new DispatcherFrame();
        Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
            new DispatcherOperationCallback(ExitFrame), frame);
        Dispatcher.PushFrame(frame);
    }

    private static object ExitFrame(object frame)
    {
        ((DispatcherFrame)frame).Continue = false;
        return null;
    }
}

When I am using the DispatcherUtil, it looks like the call to ExitFrame happens too soon, before the data is ready.

Am I not using the DispatcherUtil correctly? It seems like a better method to use to handle the dispatcher rather then waiting for callbacks from the view model.

  • 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-30T05:14:01+00:00Added an answer on May 30, 2026 at 5:14 am

    Since the dispatcher is problematic in unit tests, my solution would be to break your view-model’s dependency on the dispatcher. I assume that currently you have hard coded references like:

    Dispatcher.CurrentDispatcher.BeginInvoke(..
    

    The dispatcher is an external dependency and shouldn’t be part of your unit tests – we can assume the dispatcher works.

    I would use dependency injection (either poor mans, Unity, etc).
    Create a suitable interface representing the dispatcher.
    Create a real implementation which wraps the real dispatcher.
    Create a fake implementation which uses Action.BeginInvoke.
    In the fake you record all IAsyncResults returned to calls to BeginInvoke.
    Then have a helper method which would wait for all calls to completed which you can use in your test to wait for completion.

    Or have a view model base class which does the same thing. Calls the dispatcher normally but can be directed to call a fake during tests.

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

Sidebar

Related Questions

Im trying to follow a tutorial that is using SBJsonParser to get twitter feeds,
I'm trying to follow this tutorial using StructureMap : http://iridescence.no/post/Constructor-Injection-for-ASPNET-MVC-Action-Filters.aspx What I'm trying to
I'm trying to parse a string I get from a hashtag, and so far
I am trying to follow this article: http://chriskoenig.net/2011/08/19/upload-files-from-windows-phone/ but when I am writing: using
i'm trying to follow the suggestion here: Call ASP.NET function from JavaScript? But it's
im trying to create a xml file using php.everytime i run the code the
I'm trying to learn how to run C# and C++ code together using Mono
I'm trying to put sub-child values from XML into a dictionary collection using LINQ.
I am trying to get a very simple program to compile while using functions
I'm trying to understand shaders using libgdx, coming from an XNA/HLSL background. I'm trying

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.