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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:27:50+00:00 2026-06-01T20:27:50+00:00

I am running a MS unit test in VS2010 to test my multi-threaded application.

  • 0

I am running a MS unit test in VS2010 to test my multi-threaded application.

The application uses an AutoResetEvent to synchronise threads, which is declared like so:

private readonly AutoResetEvent captureParsedEvent = new AutoResetEvent(false);

Main test thread (ID: 13)

The main test thread launches a thread to parse a capture file, and then calls WaitOne() on a AutoResetEvent, blocking until the capture is complete:

int id = Thread.CurrentThread.ManagedThreadId;
CaptureManager.Instance.StartProcessingPackets();
Trace.WriteLine("[" + id + "]: WAITING ON CaptureParsedEvent");
captureParsedEvent.WaitOne();
Trace.WriteLine("[" + id + "]: WAITING ON CaptureParsedEvent DONE!");

// Analyse parsed capture...

(Side note: the code originally had a call to captureParsedEvent.Reset() after WaitOne() but I removed this while investigating this problem, as my research has concluded that this may not be necessary for AutoResetEvent objects.)

Parsing thread (ID: 18)

Meanwhile, the thread which is doing the parsing signals the AutoResetEvent like this:

private void InstanceManagerStateChanged(ManagerStateEventArgs ea, object sender)
{
    int id = Thread.CurrentThread.ManagedThreadId;
    switch(ea.CurrentState)
    {
        case ManagerState.ReadPacketsDone:
            Trace.WriteLine("\t[" + id + "]: CaptureParsedEvent SIGNAL");
            captureParsedEvent.Set();
            Trace.WriteLine("\t[" + id + "]: CaptureParsedEvent DONE!");
            break;
    }
}

Normally, everything behaves well and I see the following expected output in the output window:

[13]: WAITING ON CaptureParsedEvent
    [18]: CaptureParsedEvent SIGNAL
    [18]: CaptureParsedEvent DONE!
[13]: WAITING ON CaptureParsedEvent DONE!

However, I am seeing the following output intermittently:

[13]: WAITING ON CaptureParsedEvent
[13]: WAITING ON CaptureParsedEvent DONE!

This is obviously causing me issues because the capture hasn’t really finishing parsing.

The above place is the only occurrence of captureParsedEvent.Set(); so I know that nobody else is signalling the event.

A few questions:

  1. Is Trace.WriteLine() thread-safe and outputting the traces in the correct order?

  2. I have only seen this issue when running unit tests in VS2010 — is there something funny going on with tests being run in parallel, and usage of threads in this scenario that may be causing issues? My understanding is that the tests run in serial, but not sure if this is correct.

  • 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-01T20:27:51+00:00Added an answer on June 1, 2026 at 8:27 pm

    I have resolved my issue.

    It turns out that for each packet that the code was processing, it was adding the InstanceManagerStateChanged() callback to the list of delegates:

    CaptureManager.Instance.ManagerStateChanged += InstanceManagerStateChanged;
    

    but we weren’t correctly unsubscribing, which meant that I may have been getting notifications from the previous packet.

    Unsubscribing prior to processing the next packet fixed this issue:

    CaptureManager.Instance.ManagerStateChanged -= InstanceManagerStateChanged;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to automate the running of unit test as a part of
I am running some unit test on my Zend Framework application. What I cant
So I'm running a unit test, it's a bad unit test which takes a
I am trying to get a ASP.NET Unit Test running and I keep getting
I am looking to implement a continuous unit test running system, something I have
When running tests in Ruby's unit::test framework, is there a really easy way to
I was running my first Visual Studio 2008 Unit Test with a WCF Service
I am running on Castle's trunk, and trying to unit-test a controller-action where validation
I'm thinking of running unit tests for the business logic in the Lightswitch application.
When running unit and functional tests using rake, on a rails application, I notice

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.