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
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 7 Posters 2.1k 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.
  • hskoglundH Offline
    hskoglundH Offline
    hskoglund
    wrote on last edited by hskoglund
    #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

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

      GerhardG Offline
      GerhardG Offline
      Gerhard
      wrote on last edited by Gerhard
      #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
      2
      • GerhardG Gerhard

        @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 last edited by
        #5

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

        GerhardG JonBJ 3 Replies Last reply
        0
        • A abdullahzubair109

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

          GerhardG Offline
          GerhardG Offline
          Gerhard
          wrote on 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

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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 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
            0
            • A abdullahzubair109

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

              GerhardG Offline
              GerhardG Offline
              Gerhard
              wrote on 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
              • JonBJ JonB

                @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 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
                0
                • A abdullahzubair109

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

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on 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
                  0
                  • A abdullahzubair109

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

                    Kent-DorfmanK Offline
                    Kent-DorfmanK Offline
                    Kent-Dorfman
                    wrote on 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.

                    The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

                    1 Reply Last reply
                    2
                    • J.HilkJ J.Hilk

                      @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 last edited by
                      #12

                      @J.Hilk could be anything

                      1 Reply Last reply
                      0

                      • Login

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