Re: Limiting the number of instances of one application
Quote:
Originally Posted by
wysota
Trust me, you won't manage to do it in a system independent way - under the hood there is always platform specific code - even in Qt itself.
Actually QTcpSocket works quite well. I've tried a few things and right now by mixing sockets, kinda lock file and a QSemaphore I'm able to prevent the creation of more than a given number of application instance. The only problem left is that communication with the working instances is not really straightforward and, ATM, buggy.
Re: Limiting the number of instances of one application
Did you also look at the QUdpSocket? (Qt Broadcast example)
Maybe it would also work this way:
1) Application is started.
2) Application opens a Udp port that is known to all applications.
3) The new app sends out a datagramm
4) Now the app does listen for all responses from other running applications
5) The new app does verify the number of responses and determines the total number of running instances
6.1) If there are too many applications running, the new app terminates itself.
6.2) If it is possible to start another instance, then the new app will close the Udp server socket and open a Udp client socket, listening for broadcast messages itself.
This should really work on any platform.
Re: Limiting the number of instances of one application
It may work but is still an ugly solution. If you only want to make sure you have a single instance of the application, you can avoid sockets. If you want your applications on a local machine to communicate, you could come up with a better solution too. Depending on high-numbered sockets is a risk, because there is no way telling if no other app (for example some ftp client or server) doesn't use the same socket.
Why do you need a QSemaphore here? Is your app accessing the socket from multiple threads?
Re: Limiting the number of instances of one application
Quote:
Originally Posted by
wysota
It may work but is still an ugly solution. If you only want to make sure you have a single instance of the application, you can avoid sockets. If you want your applications on a local machine to communicate, you could come up with a better solution too. Depending on high-numbered sockets is a risk, because there is no way telling if no other app (for example some ftp client or server) doesn't use the same socket.
Maybe ugly but better than nothing. Besides socket corruption is highly improbable since localhost is used and the port is dynamically assigned by Qt through the server classes (QTcpServer or QUdpServer)
Quote:
Originally Posted by
wysota
Why do you need a QSemaphore here? Is your app accessing the socket from multiple threads?
The semaphore was just used as a convinient counter providing possibilities of extension to multithreaded approach...
Re: Limiting the number of instances of one application
Quote:
Originally Posted by
Chicken Blood Machine
A quick and dirty cross-platform solution would be to create a 'lock' file at startup and delete the file at shutdown. Any new instances check if the file exists first before continuing.
This isn't foolproof, since if your app crashes, you will have to delete the file manually.
IMO this is the best solution, easy and clean. It works for all platforms, it can be instrumented through the net, or locally, it can be a counter to limit number of applications...
Simple is, in many cases, the best solution...
Re: Limiting the number of instances of one application
Quote:
Originally Posted by
arnaiz
Simple is, in many cases, the best solution...
It has its strong disadvantages. There is really no way telling if the lock file is stale or not. Under Windows you can easily use a file to make a singleton application by demanding a mandatory lock on the file. On the other hand I don't know if the lock will be released when the app gets killed. So a solution is easy but the drawback is that you might not be able to restart the application after a crash. The same goes with files holding pids of processes. You can never be sure if the lock file is stale or not and eventually you have to guess (for example by trying to find the process in the process table) or ask the user (like for example kmail does). So simple is easy to implement, but can fail as easily.
Re: Limiting the number of instances of one application
Mixing kinda lock file and TCP connection is definitely the best slution IMO. I'm attaching the source I've come to. It can lock the application to whatever number of instances wanted and can send messages to the first created instances (this is a limitation I didn't managed to fix with the current design so I'm working on a new version...)
Well actually I can't attach my file (too much already attached by me on other posts : I'm waiting for an answer from the admins and will attach it ASAP :o )
Re: Limiting the number of instances of one application
Quote:
Originally Posted by
nouknouk
I only have a licensee version of Qt, nothing more. QtSingleApplication class exists since a while (before Qt 3.3.x) and it is still not licensed under GPL, so I think It won't be available for open source license soon :/
FWIW, QtSingleApplication is now available as an Open Source Edition (LGPL) here
Re: Limiting the number of instances of one application
IMHO... When I've had to limit the number of application instances in the past, I've used the port-method. With Qt it's simple to do as well. Granted, there are some problems like, what happens if another app is already using the requested port, etc. But i believe that handling those issues are simpler than creating platform specific code for locking processes or handling lock files (which in the worst case can require one or more monitoring processes to clean up after crashes etc).
Those are just my two cents, for whatever they're worth :)
Re: Limiting the number of instances of one application