How to count number of running app instances?


#1

Is there a simple way to know how many instances of my app are running?

This is NOT for a plugin – rather for a full executable application. I have allowed multiple instances by returning true in my moreThanOneInstanceAllowed() function.

Many thanks!


#2

Definitely no simple way! OSes tend to avoid letting processes spy on each other for security reasons.

Complex way… Certainly possible using NamedPipes to communicate between them all, but a reliable algorithm for a set of communicating processes to count their total may be something you’d need to look up!

(BTW the naive way of having each process add itself to a list stored in a file in a known location could work to some extent, but if processes crash they wouldn’t correctly remove themselves)


#3

Any app-instance could create a flag-file (with the process-number, date and a random number as filename) and hold a write lock on it.
The other app-instances could checking the file (and if its not-possible get the write lock, to check if the process is currently running)

Wouldn’t this be a great job for ApplicationProperties-Class, which could be extended to have consistent access the same properties on the same machine.
The first instance could be some kind of manager, which adds/removes the delta-data of the other instances.

BTW: I don’t think working with file-flags is a naive way, i think its most reliable way, because the behavior is well defined and consistent across different OS, if you working with locks.


#4

It’s really not that simple - finding out whether another process is running may not be possible, and even if it is, you then have race conditions if multiple other processes try to check and correct for missing ones. There are probably academic papers about solving this kind of problem, but it definitely isn’t something you could do reliably without a lot of thought.