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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:38:04+00:00 2026-05-12T23:38:04+00:00

Quick summary with what I now know I’ve got an EventWaitHandle that I created

  • 0

Quick summary with what I now know

I’ve got an EventWaitHandle that I created and then closed. When I try to re-create it with this ctor, an “Access to the path … is denied” exception is thrown. This exception is rare, most of the times it just re-creates the EventWaitHandle just fine. With the answer posted below (by me), I’m able to successfully call EventWaitHandle.OpenExisting and continue on in the case that an exception was thrown, however, the ctor for EventWaitHandle should have done this for me, right? Isn’t that what the out parameter, createdNew is for?


Initial question

I’ve got the following architecture, a windows service and a web service on the same server. The web service tells the windows service that it has to do work by opening and setting the wait handle that the windows service is waiting on.

Normally everything is flawless and I’m able to start / stop the windows service without any issue popping up. However, some times when I stop the web service and then start it up again, it will be completely unable to create the wait handle, breaking the whole architecture.

I specifically need to find out what is breaking the event wait handle and stop it. When the wait handle “breaks”, I have to reboot windows before it will function properly again and thats obviously not ideal.

UPDATE: Exception thrown & Log of Issue

I rebooted the windows service while the web service was doing work in hopes of causing the issue and it did! Some of the class names have been censored for corporate anonymity

12:00:41,250 [7] - Stopping execution due to a ThreadAbortException
System.Threading.ThreadAbortException: Thread was being aborted.
   at System.Threading.Thread.SleepInternal(Int32 millisecondsTimeout)
   at OurCompany.OurProduct.MyClass.MyClassCore.MonitorRequests()

12:00:41,328 [7] - Closing Event Wait Handle
12:00:41,328 [7] - Finally block reached


12:00:42,781 [6] - Application Start
12:00:43,031 [6] - Creating EventWaitHandle: Global\OurCompany.OurProduct.MyClass.EventWaitHandle
12:00:43,031 [6] - Creating EventWaitHandle with the security entity name of : Everyone

12:00:43,078 [6] - Unhandled Exception 
System.UnauthorizedAccessException: Access to the path 'Global\OurCompany.OurProduct.MyClass.EventWaitHandle' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.Threading.EventWaitHandle..ctor(Boolean initialState, EventResetMode mode, String name, Boolean& createdNew, EventWaitHandleSecurity eventSecurity)
   at OurCompany.OurProduct.MyClassLibrary.EventWaitHandleFactory.GetNewWaitHandle(String handleName, String securityEntityName, Boolean& created)
   at OurCompany.OurProduct.MyClassLibrary.EventWaitHandleFactory.GetNewEventWaitHandle()
   at OurCompany.OurProduct.MyClass.MyClassCore..ctor()

Rough timeline:

  • 11:53:09,937: The last thread on the web service to open that existing wait handle, COMPLETED its work (as in terminated connection with the client)

  • 12:00:30,234: The web service gets a new connection, not yet using the wait handle. The thread ID for this connection is the same as the thread ID for the last connection at 11:53

  • 12:00:41,250: The windows service stops

  • 12:00:42,781: The windows service starts up

  • 12:00:43,078: The windows service finished crashing

  • 12:00:50,234: The web service was actually able to open the wait handle call Set() on it without any exception thrown etc.

  • 12:02:00,000: I tried rebooting the windows service, same exception

  • 12:36:57,328: After arbitrarily waiting 36 minutes, I was able to start the windows service up without a full system reboot.


Windows Service Code

Initialization:

// I ran into security issues so I open the global EWH
//    and grant access to Everyone
var ewhSecurity = new EventWaitHandleSecurity();

ewhSecurity.AddAccessRule(
 new EventWaitHandleAccessRule(
  "Everyone",
  EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify,
  AccessControlType.Allow));

this.ewh = new EventWaitHandle(
 false,
 EventResetMode.AutoReset,
 @"Global\OurCompany.OurProduct.MyClass.EventWaitHandle",
 out created,
 ewhSecurity);

// the variable "created" is logged

Utilization:

// wait until the web service tells us to loop again
this.ewh.WaitOne();

Disposal / closing:

try
{
    while (true)
    {
        // entire service logic here
    }
}
catch (Exception e)
{
    // should this be in a finally, instead?
    if (this.ewh != null)
    {
        this.ewh.Close();
    }
}

Web Service Code

Initialization:

// NOTE: the wait handle is a member variable on the web service
this.existing_ewh = EventWaitHandle.OpenExisting(
    @"Global\OurCompany.OurProduct.MyClass.EventWaitHandle");

Utilization:

// wake up the windows service
this.existing_ewh.Set();

Since the EventWaitHandle is a member variable on the web service, I don’t have any code that specifically closes it. Actually, the only code that interacts with the EventWaitHandle on the web service is posted above.


Looking back, I should probably have put the Close() that is in the catch block, in a finally block instead. I probably should have done the same for the web service but I didn’t think that it was needed.

At any rate, can anyone see if I’m doing anything specifically wrong? Is it crucially important to put the close statements within a finally block? Do I need to manually control the Close() of the existing_ewh on the web service?

Also, I know this is a slightly complex issue so let me know if you need any additional info, I’ll be monitoring it closely and add any needed information or explanations.

Reference material

  • EventWaitHandleSecurity Class
  • EventWaitHandleAccessRule Class
  • EventWaitHandle Class
  • 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-12T23:38:05+00:00Added an answer on May 12, 2026 at 11:38 pm

    In the code that creates the wait handle on the windows service, if it fails (as in access denied), you could try to “open an existing wait handle” via

    EventWaitHandle.OpenExisting(
        @"Global\OurCompany.OurProduct.MyClass.EventWaitHandle",
        EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify);
    

    Though, I’m not entirely sure if the behaviour would stay the same at that point.

    Note: I’d appreciate feedback. Its a potential answer so I’m answering my own question, again, plenty of comments are quite welcome!

    Note 2: Amazingly, applying EventWaitHandleRights.FullControl instead of the above flags (Synchronize + Modify) doesn’t work well. You must use the sample above.

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

Sidebar

Ask A Question

Stats

  • Questions 263k
  • Answers 263k
  • 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 You need to use a closure: for (var i =… May 13, 2026 at 12:03 pm
  • Editorial Team
    Editorial Team added an answer Yes, you can use the API. The url you'll need… May 13, 2026 at 12:03 pm
  • Editorial Team
    Editorial Team added an answer All regex implementations I know of will (try to) match… May 13, 2026 at 12:03 pm

Related Questions

Quick summary with what I now know I've got an EventWaitHandle that I created
I remember the old effective approach of studying a new framework. It was always
After using the ebay API recently, I was expecting it to be as simple
I'm writing a Mahjong Game in C# (the Chinese traditional game, not the solitaire
I am building an EC website for a customer and the project manager came

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.