Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Do an operation when computer goes to sleep
QtWS25 Last Chance

Do an operation when computer goes to sleep

Scheduled Pinned Locked Moved Solved General and Desktop
signal
11 Posts 6 Posters 4.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nosba
    wrote on 20 Jul 2016, 17:18 last edited by
    #1

    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!

    ? M 2 Replies Last reply 20 Jul 2016, 17:25
    0
    • N Nosba
      20 Jul 2016, 17:18

      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!

      ? Offline
      ? Offline
      A Former User
      wrote on 20 Jul 2016, 17:25 last edited by
      #2

      @Nosba Hi! Yes, there is a D-Bus signal for this, it's PrepareForSleep(), see: https://www.freedesktop.org/wiki/Software/systemd/logind/.

      1 Reply Last reply
      8
      • N Nosba
        20 Jul 2016, 17:18

        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!

        M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 20 Jul 2016, 19:43 last edited by
        #3

        @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 received

        So seems to function.
        Tested on win 10 ONLY.

        1 Reply Last reply
        10
        • N Offline
          N Offline
          Nosba
          wrote on 22 Jul 2016, 08:27 last edited by
          #4

          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.

          M 1 Reply Last reply 22 Jul 2016, 08:42
          0
          • N Nosba
            22 Jul 2016, 08:27

            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.

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 22 Jul 2016, 08:42 last edited by
            #5

            @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.

            1 Reply Last reply
            3
            • D Offline
              D Offline
              dethtoll
              wrote on 15 Aug 2019, 18:49 last edited by
              #6

              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.

              1 Reply Last reply
              2
              • L Offline
                L Offline
                LusuQT
                wrote on 9 May 2023, 12:13 last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  LusuQT
                  wrote on 10 May 2023, 13:15 last edited by
                  #8

                  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?

                  M 1 Reply Last reply 11 May 2023, 06:06
                  0
                  • L LusuQT
                    10 May 2023, 13:15

                    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?

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 11 May 2023, 06:06 last edited by
                    #9

                    @LusuQT

                    Hi
                    I dont have a win 11 to test with but does it get the
                    WM_POWERBROADCAST event ?

                    L 1 Reply Last reply 11 May 2023, 06:25
                    0
                    • M mrjj
                      11 May 2023, 06:06

                      @LusuQT

                      Hi
                      I dont have a win 11 to test with but does it get the
                      WM_POWERBROADCAST event ?

                      L Offline
                      L Offline
                      LusuQT
                      wrote on 11 May 2023, 06:25 last edited by LusuQT 5 Nov 2023, 06:26
                      #10

                      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.

                      Pl45m4P 1 Reply Last reply 22 May 2023, 13:50
                      0
                      • Pl45m4P Pl45m4 referenced this topic on 22 May 2023, 13:37
                      • L LusuQT
                        11 May 2023, 06:25

                        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.

                        Pl45m4P Offline
                        Pl45m4P Offline
                        Pl45m4
                        wrote on 22 May 2023, 13:50 last edited by
                        #11

                        @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)


                        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                        ~E. W. Dijkstra

                        1 Reply Last reply
                        1

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved