Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Get count of running instances of my application



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


  • Lifetime Qt Champion

    @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



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



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



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



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


Log in to reply