0

I have coded an executable process that runs the following line of code:

immediateShutdownEvent = new EventWaitHandle(false, EventResetMode.ManualReset, "Global\\immediateShutdownEvent", out createdNew, security);

I can set this event while this executable process is running and it works fine, but once the executable is killed off as a process (from task manager if you manually kill the process, for example) and I automatically restart the process right after I kill it, I get a "System.UnauthorizedAccessException: Access to the path 'Global\immediateShutdownEvent' is denied" even though it runs under the same context of SYSTEM. Why is this, and how could I fix it?

Alexandru
  • 12,264
  • 17
  • 113
  • 208
  • 1
    How much time do you allow for Windows to clean up the shrapnel from the killed process? – Hans Passant Apr 12 '13 at 16:16
  • Well, is there a way I can force it to clean it up? Like, how can I force it to close this handle if its still around? – Alexandru Apr 12 '13 at 16:22
  • Like, I can see its waiting for the owner to die to kill the handle...so on a restart I can try to open the handle and force it to clean it...but I haven't been able to force it successfully yet. Any ideas? My code is probably wrong. – Alexandru Apr 12 '13 at 16:23
  • @HansPassant - Any ideas? Is there a way to wait for Windows clean-up on EventWaitHandles? – Alexandru Apr 12 '13 at 16:34
  • How much time does an EventWaitHandle need to clean up *if Close was never called on it because the process terminated*? – Alexandru Apr 12 '13 at 17:22
  • Do avoid us having to guess what your termination code looks like, post it. If you use TerminateProcess() then that only starts the termination. You also have to ensure it is complete, wait on the process handle. – Hans Passant Apr 12 '13 at 17:42
  • @HansPassant - Termination code does not get run if you terminate the process manually like I do, so no Close() is called. Its okay, I figured out how to get around it by using OpenExisting() and grabbing the WaitEventHandle...then closing it if it exists, although that being said your current account needs access rights to delete this event. – Alexandru Apr 12 '13 at 18:36

1 Answers1

0

I figured out how to get around it by using OpenExisting() and grabbing the WaitEventHandle...then closing it if it exists, although that being said your current account needs access rights to delete this event.

Alexandru
  • 12,264
  • 17
  • 113
  • 208