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
Forum Updated to NodeBB v4.3 + New Features

Do an operation when computer goes to sleep

Scheduled Pinned Locked Moved Solved General and Desktop
signal
11 Posts 6 Posters 4.4k Views 3 Watching
  • 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.
  • NosbaN Offline
    NosbaN Offline
    Nosba
    wrote on 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!

    ? mrjjM 2 Replies Last reply
    0
    • NosbaN Nosba

      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 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
      • NosbaN Nosba

        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!

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on 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
        • NosbaN Offline
          NosbaN Offline
          Nosba
          wrote on 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.

          mrjjM 1 Reply Last reply
          0
          • NosbaN Nosba

            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.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on 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 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 last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  LusuQT
                  wrote on 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?

                  mrjjM 1 Reply Last reply
                  0
                  • L LusuQT

                    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?

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 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
                    0
                    • mrjjM mrjj

                      @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 last edited by LusuQT
                      #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
                      0
                      • Pl45m4P Pl45m4 referenced this topic on
                      • L LusuQT

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