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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:28:23+00:00 2026-05-14T23:28:23+00:00

I am writing unit tests for a multi-threading application, where I need to wait

  • 0

I am writing unit tests for a multi-threading application, where I need to wait until a specific event is triggered so that I know the asynchronous operation is done. For example, when I call repository.add(something), I wait for event AfterChange before doing any assertion. So I wrote a utility function to do that:

public static void SyncAction(EventHandler event_, Action action_)
{
    var signal = new object();
    EventHandler callback = null;
    callback = new EventHandler((s, e) =>
    {
        lock (signal)
        {
          Monitor.Pulse(signal);
        }

        event_ -= callback;
    });

    event_ += callback;

    lock (signal)
    {
        action_();
        Assert.IsTrue(Monitor.Wait(signal, 10000));
    }
}

However, the compiler prevents from passing the event out of the class. Is there a way to achieve that?

Below is the solution using reflection.

public static void SyncAction(object target_, string event_, Action action_)
{
    SyncAction(
        new List<Pair<object, string>>() { new Pair<object, string>(target_, event_) },
        action_);
}

public static void SyncAction(IEnumerable<Pair<object, string>> events_, Action action_)
{
    var events = events_
        .Select(a => new Pair<object, EventInfo>(a.First, a.First.GetType().GetEvent(a.Second)))
        .Where(a => a.Second != null);

    var count = events.Count();
    var signal = new object();
    var callback = new EventHandler((s, e) =>
    {
        lock (signal)
        {
            --count;
            Monitor.Pulse(signal);
        }
    });

    events.ForEach(a => a.Second.AddEventHandler(a.First, callback));

    lock (signal)
    {
        action_();
        while (count > 0)
        {
            Assert.IsTrue(Monitor.Wait(signal, 10000));
        }
    }
    events.ForEach(a => a.Second.RemoveEventHandler(a.First, callback));
}
  • 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-14T23:28:24+00:00Added an answer on May 14, 2026 at 11:28 pm

    The problem is that events aren’t really first-class values in .NET 🙁 (Ditto properties, methods etc.)

    Reactive Extensions handles this in two ways:

    • You can provide the name of the event and the target, and it will use reflection… something like this:

      var x = Observable.FromEvent<EventArgs>(button, "Click");
      
    • You can provide a delegate for subscription and a delegate for unsubscription:

      var x = Observable.FromEvent<EventArgs>(
            handler => button.Click += handler,
            handler => button.Click -= handler);
      

    (The exact methods may be slightly different, but that’s the general idea.)

    Of course, if you’re happy to use Reactive Extensions yourself, you could use those methods and make your test use IObservable<T> 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 389k
  • Answers 389k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer We do this a lot. Here's a script that should… May 15, 2026 at 12:45 am
  • Editorial Team
    Editorial Team added an answer In JPA (and in hibernate, but I know JPA better),… May 15, 2026 at 12:45 am
  • Editorial Team
    Editorial Team added an answer Views expose the .MasterName property which specifies which master page… May 15, 2026 at 12:45 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.