Save yourself some pain. Learn C++ before learning Qt.
Someone recently came up with this kind of solution on X11.
J-P Nurmi
Of course the drawback is that once the application holding the semaphore crashes, you have no way of starting it again but reboot![]()
It's good to obtain a process ID (even though I'm pretty sure it can be done in plain Qt4) but what is important is to check that a proccess with this ID is running, as suggested...
It uses something called "semaphore". Honestly I have no idea of what it is but I know that ther is a class called QSemaphore in Qt4, maybe it would fit...Someone recently came up with this kind of solution on X11.
Current Qt projects : QCodeEdit, RotiDeCode
arnaiz (20th October 2006)
http://en.wikipedia.org/wiki/Semaphore_(programming)
It wouldn't. QSemaphore works in scope of the application, whereas the above mentioned semaphores are system-wide and are therefore suited for IPC (Inter Process Communication) which is the basic idea of using them in this situation.but I know that ther is a class called QSemaphore in Qt4, maybe it would fit...
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.
Last edited by fullmetalcoder; 19th October 2006 at 07:55. Reason: spelling error
Current Qt projects : QCodeEdit, RotiDeCode
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.
fullmetalcoder (20th October 2006)
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?
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)
The semaphore was just used as a convinient counter providing possibilities of extension to multithreaded approach...
Current Qt projects : QCodeEdit, RotiDeCode
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![]()
Last edited by TheRonin; 11th May 2009 at 10:33. Reason: Accidentally posted.
Bookmarks