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. Get count of running instances of my application

Get count of running instances of my application

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 780 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.
  • gde23G Offline
    gde23G Offline
    gde23
    wrote on last edited by
    #1

    Hi there,

    how can I get the count of runnung instances of my application. I've taken a look into QtSingleApplication and QSystemSemaphore, but as far as I could find out both only can block multiple (or more than allowd) instances of the application but not tell me the count? However internally they somehow have to know the count, so how is it done?

    jsulmJ JonBJ 2 Replies Last reply
    0
    • gde23G gde23

      Hi there,

      how can I get the count of runnung instances of my application. I've taken a look into QtSingleApplication and QSystemSemaphore, but as far as I could find out both only can block multiple (or more than allowd) instances of the application but not tell me the count? However internally they somehow have to know the count, so how is it done?

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @gde23 said in Get count of running instances of my application:

      However internally they somehow have to know the count, so how is it done?

      You can check their source code

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • gde23G gde23

        Hi there,

        how can I get the count of runnung instances of my application. I've taken a look into QtSingleApplication and QSystemSemaphore, but as far as I could find out both only can block multiple (or more than allowd) instances of the application but not tell me the count? However internally they somehow have to know the count, so how is it done?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @gde23
        They do not need to know a "count". They only need to know whether the resource is currently locked or not, which is a simpler algorithm.

        I haven't looked at their code. I don't know what Qt class you might use, and there are doubtless other approaches. But one way I have used (non-Qt code) is to pick a shared resource --- memory, file --- and have an attaching process lock a byte while it is active (automatically freed on process exit). Now a process which wants to count attached processes can test locking bytes (non-blocking) in a loop to discover how many are currently locked.

        Alternatively, a less reliable but "easy" approach is to enumerate the names of running processes and count those with your name.

        1 Reply Last reply
        3
        • gde23G Offline
          gde23G Offline
          gde23
          wrote on last edited by
          #4

          @jsulm: I tried that, but its really hard to understand what is going on there.
          @JonB: Thanks for suggestion with the shared resource. However that will not work when my application crashes.

          Maybe I need to tell a bit more what I want to achieve:

          I want to have a simple autosave. At the moment I've implemented to save a file every few minutes to the temp folder. If the application ends correctly it will delete that file. If it crashes the file is still there so on next start it can ask the user if he wants to open it to restore the last save before the crash. This works totally fine, however when I open a second instance of the application now there of course is the autosave file from the first one, so it needs to know that that one belongs to the other instance.

          JonBJ 1 Reply Last reply
          0
          • gde23G gde23

            @jsulm: I tried that, but its really hard to understand what is going on there.
            @JonB: Thanks for suggestion with the shared resource. However that will not work when my application crashes.

            Maybe I need to tell a bit more what I want to achieve:

            I want to have a simple autosave. At the moment I've implemented to save a file every few minutes to the temp folder. If the application ends correctly it will delete that file. If it crashes the file is still there so on next start it can ask the user if he wants to open it to restore the last save before the crash. This works totally fine, however when I open a second instance of the application now there of course is the autosave file from the first one, so it needs to know that that one belongs to the other instance.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @gde23 said in Get count of running instances of my application:

            However that will not work when my application crashes.

            Why not?? I did write

            (automatically freed on process exit)

            so, yes, the lock held by an instance is freed even if it crashes. Have used this in production code for > 20 years....

            Indeed, using a temporary file the way you describe will be no good on crash. (Though your OS may support a temporary file flag which does get deleted on exit/crash, but it won't be the default flag, if that's what you want to pursue.)

            so it needs to know that that one belongs to the other instance

            You can't do this. How can you distinguish between (a) I am another instance so must not open the existing file versus (b) I am an instance run now after some other instance crashed in the past so I am allowed to open the file?

            You need to rethink your approach.

            P.S.
            From what you describe it looks like you are over-complicating. You don't need to count instances. I think all you are saying you want is: process open autosave file exclusively or with lock. Then keep that file handle open throughout its creating process. That can be used to stop another instance from opening/reading/using it. When the first process exits or crashes, the exclusivity/lock will be automatically released.

            1 Reply Last reply
            3
            • gde23G Offline
              gde23G Offline
              gde23
              wrote on last edited by
              #6

              @JonB Thanks a lot for the explanation.
              And you are totally right, I was just over complicating it because I didn't come up with a better solution than the counting (which was a bad idea i guess)
              So I will go with the solution with the exclusive lock you suggest.
              That's a much better approach :)

              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