title: My EventWaitHandle says "Access to the path is denied", but its not
body: <h2>Quick summary with what I now know</h2>
  
  <p>I've got an <code>EventWaitHandle</code> that I created and then closed. When I try to re-create it with <strong><a href="http://msdn.microsoft.com/en-us/library/z4c9z2kt.aspx" rel="noreferrer">this ctor</a></strong>, an "Access to the path ... is denied" exception is thrown.  This exception is rare, most of the times it just re-creates the <code>EventWaitHandle</code> just fine.  With the answer posted below (by me), I'm able to successfully call <code>EventWaitHandle.OpenExisting</code> and continue on in the case that an exception was thrown, however, the ctor for <code>EventWaitHandle</code> should have done this for me, right?  Isn't that what the <a href="http://msdn.microsoft.com/en-us/library/z4c9z2kt.aspx" rel="noreferrer"><strong>out parameter</strong></a>, <code>createdNew</code> is for?</p>
  
  <hr>
  
  <h2>Initial question</h2>
  
  <p>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.</p>
  
  <p>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.  </p>
  
  <p>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.</p>
  
  <h2>UPDATE: Exception thrown &amp; Log of Issue</h2>
  
  <p>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</p>
  
  <pre><code>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&amp; createdNew, EventWaitHandleSecurity eventSecurity)
     at OurCompany.OurProduct.MyClassLibrary.EventWaitHandleFactory.GetNewWaitHandle(String handleName, String securityEntityName, Boolean&amp; created)
     at OurCompany.OurProduct.MyClassLibrary.EventWaitHandleFactory.GetNewEventWaitHandle()
     at OurCompany.OurProduct.MyClass.MyClassCore..ctor()
  </code></pre>
  
  <p><strong>Rough timeline:</strong></p>
  
  <ul>
  <li><p>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)</p></li>
  <li><p>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</p></li>
  <li><p>12:00:41,250: The windows service stops</p></li>
  <li><p>12:00:42,781: The windows service starts up</p></li>
  <li><p>12:00:43,078: The windows service finished crashing</p></li>
  <li><p>12:00:50,234: The web service was actually able to open the wait handle call Set() on it without any exception thrown etc.</p></li>
  <li><p>12:02:00,000: I tried rebooting the windows service, same exception</p></li>
  <li><p>12:36:57,328: After arbitrarily waiting 36 minutes, I was able to start the windows service up without a full system reboot.  </p></li>
  </ul>
  
  <hr>
  
  <h2><strong>Windows Service Code</strong></h2>
  
  <p>Initialization:</p>
  
  <pre><code>// 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
  </code></pre>
  
  <p>Utilization:</p>
  
  <pre><code>// wait until the web service tells us to loop again
  this.ewh.WaitOne();
  </code></pre>
  
  <p>Disposal / closing:</p>
  
  <pre><code>try
  {
      while (true)
      {
          // entire service logic here
      }
  }
  catch (Exception e)
  {
      // should this be in a finally, instead?
      if (this.ewh != null)
      {
          this.ewh.Close();
      }
  }
  </code></pre>
  
  <hr>
  
  <h2><strong>Web Service Code</strong></h2>
  
  <p>Initialization:</p>
  
  <pre><code>// NOTE: the wait handle is a member variable on the web service
  this.existing_ewh = EventWaitHandle.OpenExisting(
      @"Global\OurCompany.OurProduct.MyClass.EventWaitHandle");
  </code></pre>
  
  <p>Utilization:</p>
  
  <pre><code>// wake up the windows service
  this.existing_ewh.Set();
  </code></pre>
  
  <p>Since the <code>EventWaitHandle</code> 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 <code>EventWaitHandle</code> on the web service is posted above.</p>
  
  <hr>
  
  <p>Looking back, I should probably have put the <code>Close()</code> that is in the <code>catch</code> block, in a <code>finally</code> block instead.  I probably should have done the same for the web service but I didn't think that it was needed.</p>
  
  <p>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 <code>Close()</code> of the <code>existing_ewh</code> on the web service?</p>
  
  <p>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.</p>
  
  <p>Reference material</p>
  
  <ul>
  <li><a href="http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.eventwaithandlesecurity.aspx" rel="noreferrer">EventWaitHandleSecurity Class</a></li>
  <li><a href="http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.eventwaithandleaccessrule.aspx" rel="noreferrer">EventWaitHandleAccessRule Class</a></li>
  <li><a href="http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.aspx" rel="noreferrer">EventWaitHandle Class</a></li>
  </ul>