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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:32:33+00:00 2026-05-14T04:32:33+00:00

We were surprised to learn today that threads waiting on a ManualResetEvent continue waiting

  • 0

We were surprised to learn today that threads waiting on a ManualResetEvent continue waiting on the event even when it’s closed. We would have expected that calling Close() would implicitly signal the waiting threads.

We tracked this down as a reason some of our windows services were not shutting down as fast as we’d like. We’re changing all of our Dispose implementations that close ManualResetEvent references to call Set first.

Can anyone explain why Close doesn’t implicitly call Set? When would you want a waiting thread to continue waiting?

Here’s our test code to demonstrate our findings:

    private static readonly Stopwatch _timer = Stopwatch.StartNew();

    public static void Test()
    {

        var sync = new ManualResetEvent(false);

        ThreadPool.QueueUserWorkItem(state =>
                                         {
                                             Log("ThreadPool enter, waiting 250ms...");
                                             sync.WaitOne(250);
                                             Log("ThreadPool exit");
                                         });

        Log("Main sleeping 100");
        Thread.Sleep(100);
        Log("Main about to close");
        // sync.Set();      // Is Set called implicitly?  No...
        sync.Close();

        Log("Main waiting for exit 500ms");
        Thread.Sleep(500);
    }

    private static void Log(string text)
    {
        Console.WriteLine("{0:0} {1}", _timer.ElapsedMilliseconds, text);  
    }

When we run this code with the Set call commented, we get this..

0 Main sleeping 100
0 ThreadPool enter, waiting 250ms...
103 Main about to close
103 Main waiting for exit 500ms
259 ThreadPool exit

When we explicitly call Set we get this..

0 Main sleeping 100
0 ThreadPool enter, waiting 250ms...
98 Main about to close
98 ThreadPool exit
98 Main waiting for exit 500ms
  • 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-14T04:32:34+00:00Added an answer on May 14, 2026 at 4:32 am

    Close is a means of disposing of the object (Close and Dispose on this class yield identical behavior). It does not affect the state of the handle. To assume that, in all cases, the user would want a thread waiting on a handle that I closed to continue does not seem reasonable. In fact, the fact that the handle is in use should indicate that you shouldn’t be calling Close in the first place.

    It’s not a matter of “why shouldn’t Set be called implicitly?”, it’s a conceptual problem: If you’re calling Close, you should no longer care about the object. Use Set and Reset to control the flow of execution among threads; don’t call Close (or Dispose) on any object, WaitHandles included, until they are no longer in use.

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

Sidebar

Related Questions

I was surprised to learn today that PHP is used widely in high-traffic websites.
I was surprised to learn that a single domain name can have many IP
I was surprised recently to find that it's possible to have a return statement
I'm always surprised that even after using C# for all this time now, I
I was surprised to learn that ref and out parameters are not marked by
I was surprised to learn that R doesn't come with a handy function to
I was pleasantly surprised a few months ago to learn that I could use
I was very surprised that I couldn't find this answer on Google. I have
I was surprised to to learn that a class variable of a subclass can't
I am surprised that the following calls result in type-cast warning: I have a

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.