Results 1 to 20 of 30

Thread: Limiting the number of instances of one application

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 Limiting the number of instances of one application

    I'm trying to limit the number of running instances of an application, which is mostly useful when dealing with file registration...

    I've had a quick look at Assistant which seems to use a client/server architecture to achieve that (through QTcpSocket if I remember well) and I didn't understand how it worked... Any explanation ?
    Current Qt projects : QCodeEdit, RotiDeCode

  2. #2
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limiting the number of instances of one application

    There is a Qt solution for this... and KDE does it (I seem to recall). Perhaps you can find simple solutions there...

  3. #3
    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

    Well you could use a computer resource like a socket - you suggested that already. Pick a range of e.g. 5 sockets that your app would try to open. maybe 40000...40004. When your app is starting it will try to open such a socket. If it fails it will open the next in that range. If no socket in that range is available then your app would not start. Maybe you even enhance the socket logic that way, that you can send a simple command to it and wait for a known response. If that response doesn't come, then you know it's not your app, but the app from somebody else...
    Does this make sense?
    If sockets is not what you want to use, you may create something else like a named message queue or pipe... or a kernel mutex etc... there are several solutions to this...

  4. #4
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Limiting the number of instances of one application

    Isn't this what DCOP is for in KDE? Don't know what Windows uses or if Qt has it's own abstraction for handling stuff like this.

  5. #5
    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 Mike View Post
    Well you could use a computer resource like a socket - you suggested that already. Pick a range of e.g. 5 sockets that your app would try to open. maybe 40000...40004. When your app is starting it will try to open such a socket. If it fails it will open the next in that range. If no socket in that range is available then your app would not start. Maybe you even enhance the socket logic that way, that you can send a simple command to it and wait for a known response. If that response doesn't come, then you know it's not your app, but the app from somebody else...
    Does this make sense?
    If sockets is not what you want to use, you may create something else like a named message queue or pipe... or a kernel mutex etc... there are several solutions to this...
    There are two problems : I don't have any experience of socket programming and I want a cross-platform solution.
    Moreover I don't think I have enough time to search for that feature into the whole KDE sources and then port that Qt3 (and maybe KDE kernel)-dependent code to plain and portable Qt4.
    You talked about "socket ranges", could you explain that to me alittle deeper? As I said I'm a noob in socket-programming...
    Current Qt projects : QCodeEdit, RotiDeCode

  6. #6
    Join Date
    Feb 2006
    Posts
    26
    Thanked 2 Times in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Limiting the number of instances of one application

    Hi,

    for registered customers, there is a special class to ensure only one instance of your application is running : QtSingleApplication (it exists for both Qt3 and Qt4 versions).

    This class is unfortunately unavailable for open-source licensee :-/

  7. #7
    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 nouknouk View Post
    This class is unfortunately unavailable for open-source licensee :-/
    Unfortunately my software is meant to be Open Source thus compilable against Qt Open source so I can't rely on something like this. Neither can I "steal" its code so I'm looking for an equivalent solution in plain cross-platform and open source Qt 4. If you've heard about this class maybe you could explain me roughly how it works???
    Current Qt projects : QCodeEdit, RotiDeCode

  8. #8
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Limiting the number of instances of one application

    My guess is that their QtSingleApplication would do something different for each OS, as there's nothing preventing Qt from doing whatever it wants, as long as it achieves the "single app" functionality. In Windows, it can be fairly easily done with their own named pipes or named mutexes (not an option for other os's) - I'm sure other OS's have their own efficient way of accomplishing the "single app".

    Using sockets not be very elegant as it pollutes the port number-space, and in the worst case, may have to check several port numbers - but for the end-user, it may still be suitable and sufficiently portable.
    Software Engineer



  9. #9
    Join Date
    Feb 2006
    Posts
    26
    Thanked 2 Times in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Limiting the number of instances of one application

    Hi,

    about general concepts of QtSingleApplication, there are 3 different ways the mutex is implemented, depending on the OS used (Win, MacOS, Linux) :

    -on windows, the CreateMutex function is used, the time a call to FindWindow is done. If the window is already existing, that means an instance is already started. The mutex is released just after in all cases.

    - on linux, the Atom feature is used to implement the mutex. The window is then retrieved with something like findWindow

    - on mac ... i don't know

    QtsingleApplication gives another feature : the ability to send a message to the already running instance. Usefull, for example, when the second application's instance wants to send a filename to the first one in order to open a document. This mechanism is implemented using platform specific ways to post a message in their event loops.

  10. #10
    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

    Are "regular" Qt mutexes/threads/... able to perform this task or am I forced to use plenty of dirty platform-dependent code (which I don't know well BTW)?

    @nouknouk : you seem to be quite informed about QtSingleAppication. Do you think Trolltech plans to release it under GPL?
    Current Qt projects : QCodeEdit, RotiDeCode

  11. #11
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Limiting the number of instances of one application

    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.
    Save yourself some pain. Learn C++ before learning Qt.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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 Chicken Blood Machine View Post
    This isn't foolproof, since if your app crashes, you will have to delete the file manually.
    You can overcome that problem for example by checking if a process which created the file exists (the file can contain the process id of the creator). This still isn't 100% sure, but improves the situation a little.

  13. #13
    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

    I know I can use process ID under Unix platform but what about others, especially Windows? Actually, even if it's not the most straightforward I think the sockect stuff is the best... I'll try to understand designerassistant sources and, well, we'll see...
    Current Qt projects : QCodeEdit, RotiDeCode

  14. #14
    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...

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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.

  16. #16
    Join Date
    Feb 2006
    Posts
    26
    Thanked 2 Times in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Limiting the number of instances of one application

    Quote Originally Posted by fullmetalcoder View Post
    @nouknouk : you seem to be quite informed about QtSingleAppication. Do you think Trolltech plans to release it under GPL?
    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 :/

    Actually, even if it's not the most straightforward I think the sockect stuff is the best... I'll try to understand designerassistant sources and, well, we'll see...
    Another issue with the use of QSocket is that the user may have a firewall that doesn't allow to open listening socket.

  17. #17
    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

  18. #18
    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.