Do an operation when computer goes to sleep
-
wrote on 20 Jul 2016, 17:18 last edited by
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!
-
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!
wrote on 20 Jul 2016, 17:25 last edited by@Nosba Hi! Yes, there is a D-Bus signal for this, it's
PrepareForSleep()
, see: https://www.freedesktop.org/wiki/Software/systemd/logind/. -
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
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. -
wrote on 22 Jul 2016, 08:27 last edited by
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.
-
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.
@Nosba
Yes its very platform bound.
Also, if you need to support Vista,
you need extra code as far as the MS docs is correct. -
wrote on 15 Aug 2019, 18:49 last edited by
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.
-
wrote on 9 May 2023, 12:13 last edited byThis post is deleted!
-
wrote on 10 May 2023, 13:15 last edited by
Hi, I am using the above code in Windows 11 (Laptop).
I am not receiving the PBT_APMSUSPEND event when my Laptop is put on sleep mode.
Is there a solution that works for Laptop systems? -
Hi, I am using the above code in Windows 11 (Laptop).
I am not receiving the PBT_APMSUSPEND event when my Laptop is put on sleep mode.
Is there a solution that works for Laptop systems?Hi
I dont have a win 11 to test with but does it get the
WM_POWERBROADCAST event ? -
wrote on 11 May 2023, 06:25 last edited by LusuQT 5 Nov 2023, 06:26
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.
-
P Pl45m4 referenced this topic on 22 May 2023, 13:37
-
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.
wrote on 22 May 2023, 13:50 last edited by@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)