title: Is it possible to kill WaitForSingleObject(handle, INFINITE)?
body: <p>I am having problems closing an application that uses WaitForSingleObject() with an INFINITE timout.</p>
  
  <p>The full picture is this.  I am doing the following to allow my application to handle the device wakeup event:</p>
  
  <p>Register the event with:</p>
  
  <pre><code>CeRunAppAtEvent("\\\\.\\Notifications\\NamedEvents\\WakeupEvent",
      NOTIFICATION_EVENT_WAKEUP);
  </code></pre>
  
  <p>Start a new thread to wait on:</p>
  
  <pre><code>Thread waitForWakeThread = new Thread(new ThreadStart(WaitForWakeup));
  waitForWakeThread.Start();
  </code></pre>
  
  <p>Then do the following in the target method:</p>
  
  <pre><code>private void WaitForWakeup()
  {
      IntPtr handle = CreateEvent(IntPtr.Zero, 0, 0, "WakeupEvent");
      while (true)
      {
          WaitForSingleObject(handle, INFINITE);
          MessageBox.Show("Wakey wakey");
      }
  }
  </code></pre>
  
  <p>This all works fine until I try to close the application when, predictably, WaitForSingleObject continues to wait and does not allow the app to close properly.  We only allow one instance of our app to run at a time and we check for this on startup.  It appears to continue running until the device is soft reset.</p>
  
  <p>Is there a way to kill the handle that WaitForSingleObject is waiting for, to force it to return?</p>
  
  <p>Many thanks. </p>