4

Is there any way to find out when Windows will enter sleep mode/is in sleep mode?

Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211

1 Answers1

6

If you're using managed code then this is exposed in the SystemEvents.PowerModeChanged event.

SystemEvents.PowerModeChanged += OnPowerModeChanged;

private void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e) {
  if (e.Mode == PowerModes.Suspend) {
    // Going to sleep
  }
}

If you're using native code then you want to listen for the WM_POWERBROADCAST message in your WindowProc handler.

LRESULT WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
  if (WM_POWERBROADCAST == message && PBT_APMSUSPEND == wParam) {
    // Going to sleep
  }
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Beat me to it in your edit. I was posting this after your first answer just mentioned managed code. :) +1 and deleting my answer. – Ken White Mar 01 '12 at 16:37