Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30

Thread: Limiting the number of instances of one application

  1. #21
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limiting the number of instances of one application

    Quote Originally Posted by wysota View Post
    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

  2. #22
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    52
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  3. The following user says thank you to Mike for this useful post:

    fullmetalcoder (20th October 2006)

  4. #23
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

  5. #24
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limiting the number of instances of one application

    Quote Originally Posted by wysota View Post
    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 View Post
    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...
    Current Qt projects : QCodeEdit, RotiDeCode

  6. #25
    Join Date
    Mar 2006
    Location
    Mexico City
    Posts
    31
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limiting the number of instances of one application

    Quote Originally Posted by Chicken Blood Machine View Post
    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...

  7. #26
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Limiting the number of instances of one application

    Quote Originally Posted by arnaiz View Post
    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.

  8. #27
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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 )
    Current Qt projects : QCodeEdit, RotiDeCode

  9. #28
    Join Date
    Dec 2008
    Location
    Qt Reference Documentation
    Posts
    62
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Limiting the number of instances of one application

    Quote Originally Posted by nouknouk View Post
    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

  10. #29
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    99
    Thanks
    11
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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
    Last edited by TheRonin; 11th May 2009 at 10:33. Reason: Accidentally posted.

  11. #30
    Join Date
    Apr 2008
    Posts
    104
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limiting the number of instances of one application

    Try the SingleApplication library:
    http://www.qt-apps.org/content/show....?content=81163

Similar Threads

  1. Replies: 3
    Last Post: 8th December 2006, 18:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.