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. how to find out if application started for first time after boot
Servers for Qt installer are currently down

how to find out if application started for first time after boot

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 7 Posters 1.6k Views 1 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.
  • A Offline
    A Offline
    abdullahzubair109
    wrote on 11 May 2019, 12:00 last edited by
    #1

    i have a text editor.. every time i start this after boot, i want to run some underlying background process .. for that i must know if the text editor started for first time after boot.. how can i implement such feature???

    A G 2 Replies Last reply 11 May 2019, 12:32
    0
    • A abdullahzubair109
      11 May 2019, 12:00

      i have a text editor.. every time i start this after boot, i want to run some underlying background process .. for that i must know if the text editor started for first time after boot.. how can i implement such feature???

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 11 May 2019, 12:32 last edited by
      #2

      @abdullahzubair109

      Not directly your use case, but QtSingleApplication might give you some insight how to do it.

      Regards

      Qt has to stay free or it will die.

      1 Reply Last reply
      1
      • H Offline
        H Offline
        hskoglund
        wrote on 11 May 2019, 12:59 last edited by hskoglund 5 Nov 2019, 13:48
        #3

        If you're on Windows, you could use the ::GetTickCount64(), it returns # of milliseconds since boot.

        Use this number to calculate the date and time for the most recent boot, like this:

        QDateTime dtBoot = QDateTime::currentDateTime().addMSecs(0 - ::GetTickCount64());
        

        (need to #include "windows.h" )

        Then save this date/time in a disk file, for example an .ini file, when you start the text editor. Then before you save, you check if the previous, saved boot time is the same as the current. If not, the text editor has started for the first time since boot.

        Edit: I tested my idea, spotted one problem: the exact number of milliseconds varies due to rounding in ::GetTickCount64(), if I run my test program with a couple of minutes apart each time, it will say the boot time of my computer is:

        2019-05-11 06:35:54.786
        2019-05-11 06:35:54.776
        2019-05-11 06:35:54.783
        2019-05-11 06:35:54.780
        2019-05-11 06:35:54.784

        So some form of rounding to the nearest second needs to be applied...

        1 Reply Last reply
        3
        • A abdullahzubair109
          11 May 2019, 12:00

          i have a text editor.. every time i start this after boot, i want to run some underlying background process .. for that i must know if the text editor started for first time after boot.. how can i implement such feature???

          G Offline
          G Offline
          Gerhard
          wrote on 11 May 2019, 16:07 last edited by Gerhard 5 Nov 2019, 18:44
          #4

          @abdullahzubair109
          Try it with a "Volatile Registry Key". These entries are not persistent and would be filled at System start. If your app creates this entry than you can check if this entry is present, if not then the app was not started yet.

          Gerhard

          (sorry for my english)

          Gerhard

          A 1 Reply Last reply 14 May 2019, 10:41
          2
          • G Gerhard
            11 May 2019, 16:07

            @abdullahzubair109
            Try it with a "Volatile Registry Key". These entries are not persistent and would be filled at System start. If your app creates this entry than you can check if this entry is present, if not then the app was not started yet.

            Gerhard

            (sorry for my english)

            A Offline
            A Offline
            abdullahzubair109
            wrote on 14 May 2019, 10:41 last edited by
            #5

            @Gerhard that sounds a good approach.. but i am on a linux machine.. will try to figure out something alternative.

            G J 3 Replies Last reply 14 May 2019, 20:08
            0
            • A abdullahzubair109
              14 May 2019, 10:41

              @Gerhard that sounds a good approach.. but i am on a linux machine.. will try to figure out something alternative.

              G Offline
              G Offline
              Gerhard
              wrote on 14 May 2019, 20:08 last edited by
              #6

              @abdullahzubair109
              create a Semaphore at first start of the process, in your app check if Semaphore is created or locked. Not so familar with the new POSIX semaphores, look at the man pages or Google Linux IPC (inter process communication).

              Gerhard

              Gerhard

              1 Reply Last reply
              1
              • A abdullahzubair109
                14 May 2019, 10:41

                @Gerhard that sounds a good approach.. but i am on a linux machine.. will try to figure out something alternative.

                J Offline
                J Offline
                JonB
                wrote on 14 May 2019, 20:44 last edited by JonB
                #7

                @abdullahzubair109
                For quick & dirty (doubtless @Gerhard's semaphore is better), can't you just create a file in /tmp and check for its existence? Doesn't your /tmp get cleared out on reboot?

                A 1 Reply Last reply 15 May 2019, 07:41
                0
                • A abdullahzubair109
                  14 May 2019, 10:41

                  @Gerhard that sounds a good approach.. but i am on a linux machine.. will try to figure out something alternative.

                  G Offline
                  G Offline
                  Gerhard
                  wrote on 15 May 2019, 06:34 last edited by
                  #8

                  @abdullahzubair109
                  You can also start the Background process once at System start. Therefore you can use a script in the etc/rc Directory.

                  Gerhard

                  Gerhard

                  1 Reply Last reply
                  0
                  • J JonB
                    14 May 2019, 20:44

                    @abdullahzubair109
                    For quick & dirty (doubtless @Gerhard's semaphore is better), can't you just create a file in /tmp and check for its existence? Doesn't your /tmp get cleared out on reboot?

                    A Offline
                    A Offline
                    abdullahzubair109
                    wrote on 15 May 2019, 07:41 last edited by
                    #9

                    @JonB it depends on distros..not all distros remove temp directory on boot

                    J.HilkJ Kent-DorfmanK 2 Replies Last reply 15 May 2019, 07:44
                    0
                    • A abdullahzubair109
                      15 May 2019, 07:41

                      @JonB it depends on distros..not all distros remove temp directory on boot

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on 15 May 2019, 07:44 last edited by
                      #10

                      @abdullahzubair109
                      how much influence do you have about the Linux System.

                      Is it a custom one or could it be anything, depending on the user of your program ?


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      A 1 Reply Last reply 16 May 2019, 19:33
                      0
                      • A abdullahzubair109
                        15 May 2019, 07:41

                        @JonB it depends on distros..not all distros remove temp directory on boot

                        Kent-DorfmanK Offline
                        Kent-DorfmanK Offline
                        Kent-Dorfman
                        wrote on 15 May 2019, 08:23 last edited by
                        #11

                        @abdullahzubair109

                        If they are configured correctly, all modern linux distros should clear the /tmp directory on reboot. In fact, most of them mount /tmp as a RAM filesystem, so it is volatile anyways. The stub file under /tmp or /var/tmp or /var/run is the historical way in UNIX to check whether something is already running.

                        1 Reply Last reply
                        2
                        • J.HilkJ J.Hilk
                          15 May 2019, 07:44

                          @abdullahzubair109
                          how much influence do you have about the Linux System.

                          Is it a custom one or could it be anything, depending on the user of your program ?

                          A Offline
                          A Offline
                          abdullahzubair109
                          wrote on 16 May 2019, 19:33 last edited by
                          #12

                          @J.Hilk could be anything

                          1 Reply Last reply
                          0

                          1/12

                          11 May 2019, 12:00

                          • Login

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