Notification when Windows enters in sleep mode

993 views Asked by At

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

1

There are 1 answers

1
JaredPar On BEST ANSWER

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
  }
}