title: How to control power button press shutdown?
body: <p>I'm working on a kiosk style application where I need to control the shutdown/restart of the PC when the power button is pressed.  Thanks to this <a href="https://stackoverflow.com/questions/11242232/wm-queryendsession-is-causing-me-problems">post</a>, I'm about 90% of the way there.</p>
  
  <ol>
  <li>In control panel set the acpi power button press action to shutdown.</li>
  <li>Listen for the WndProc message WM_QUERYENDSESSION</li>
  <li><p>When received issue the completely undocumented:</p>
  
  <pre><code>[DllImport("user32.dll", SetLastError = true)]
  static extern int CancelShutdown();
  </code></pre></li>
  <li><p>Return from the WndProc and bring up my own message box asking the user to Shutdown / Restart or Cancel, and respond to their action.</p></li>
  </ol>
  
  <p>Everything works well if I do a start / shutdown from the task bar (I can issue theses as fast as I want).  Everything also works well the first time I press the power button.  On subsequent power button presses though I see a minute or so delay before I receive the WM_QUERYENDSESSION message.</p>
  
  <p>Is there a setting or registry entry about how often windows will issue an ACPI event?  I know it's not the hardware because under linux the same machine will fire the ACPI event as fast as I can press the button.</p>
  
  <p>Thanks.</p>