Do an operation when computer goes to sleep
-
Hi all,
I'm writing a program that has a timer. When the computer goes to sleep I need to save some datas in order to correct the timer's value when the computer awakes.
Are there some signals that are generated when the computer goes to sleep?
thanks in advance!
-
@Nosba Hi! Yes, there is a D-Bus signal for this, it's
PrepareForSleep()
, see: https://www.freedesktop.org/wiki/Software/systemd/logind/. -
@Nosba
hi
on windows, you can catch the WM_POWERBROADCAST
Tested with following code:#include <QAbstractNativeEventFilter> #include <QAbstractEventDispatcher> #include <QDebug> #include <windows.h> class MyEventFilter : public QAbstractNativeEventFilter { public: virtual bool nativeEventFilter(const QByteArray& eventType, void* message, long*) Q_DECL_OVERRIDE { MSG* msg = static_cast< MSG* >( message ); if (msg->message == WM_POWERBROADCAST) { switch (msg->wParam) { case PBT_APMPOWERSTATUSCHANGE: qDebug() << ("PBT_APMPOWERSTATUSCHANGE received\n"); break; case PBT_APMRESUMEAUTOMATIC: qDebug() << ("PBT_APMRESUMEAUTOMATIC received\n"); break; case PBT_APMRESUMESUSPEND: qDebug() << ("PBT_APMRESUMESUSPEND received\n"); break; case PBT_APMSUSPEND: qDebug() << ("PBT_APMSUSPEND received\n"); break; } } return false; } };
and in mainwindow constructor
QAbstractEventDispatcher::instance()->installNativeEventFilter(new MyEventFilter);When i issue
rundll32.exe powrprof.dll,SetSuspendState 0,1,0
it goes to sleep and i wake it again
i get
PBT_APMSUSPEND received
PBT_APMRESUMESUSPEND received
PBT_APMRESUMEAUTOMATIC receivedSo seems to function.
Tested on win 10 ONLY. -
Hello,
thank you all, I was looking for something that would allow me to save my data on any operating system, but it seems there are different solutions for each operating system. I'll have to write a class with conditional compilation to save my data.
thank you all once again.
-
This topic is a few years old, but still appears to be the best approach (on Windows) in 2019.
One important note:
The MS docs state "An application should return TRUE if it processes this message."Based on experimentation, it seems that you should return true in your filter on every WM_POWERBROADCAST message. Without that, I was seeing multiple repeated suspend events and resume events.
-
Yes @mrjj, It has the WM_POWERBROADCAST event. I've tried plugging my power cable to test whether PBT_APMPOWERSTATUSCHANGE is working or not. It worked perfectly, But when I put my laptop to sleep it does not capture the PBT_APMSUSPEND event. The same code worked well in Windows 11 Desktop system.
-
-
@LusuQT said in Do an operation when computer goes to sleep:
But when I put my laptop to sleep it does not capture the PBT_APMSUSPEND event.
Make sure that your laptop's power management is configured the same way as the Desktop PC's.
Check, if your laptop doesn't go into hibernate instead of suspend, when "going to sleep" (either by keyboard shortcut or from menu).
I'm not sure if hibernate also triggers the SUSPEND event.
The current power settings can also vary when using different powerplans (I think it's called like that?!).(Just a guess that this might be the difference)