Page 1 of 2 12 LastLast
Results 1 to 20 of 30

Thread: Limiting the number of instances of one application

  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,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 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
    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 fullmetalcoder View Post
    I know I can use process ID under Unix platform but what about others, especially Windows?
    I'm sure there is something like a process id on Windows as well.

    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...
    What if some other process is already using those sockets? It might be impossible to start even a single instance of the application. Probably the proper way to go would be to use semaphores and/or shared memory. It's just a few lines of platform dependent code.

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

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

    Quote Originally Posted by fullmetalcoder View Post
    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...
    http://msdn.microsoft.com/library/de...tprocessid.asp
    Save yourself some pain. Learn C++ before learning Qt.

  17. #17
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Limiting the number of instances of one application

    Someone recently came up with this kind of solution on X11.
    J-P Nurmi

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

    Of course the drawback is that once the application holding the semaphore crashes, you have no way of starting it again but reboot

  19. #19
    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 Chicken Blood Machine View Post
    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...

    Someone recently came up with this kind of solution on X11.
    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...
    Current Qt projects : QCodeEdit, RotiDeCode

  20. The following user says thank you to fullmetalcoder for this useful post:

    arnaiz (20th October 2006)

  21. #20
    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 fullmetalcoder View Post
    It uses something called "semaphore". Honestly I have no idea of what it is
    http://en.wikipedia.org/wiki/Semaphore_(programming)


    but I know that ther is a class called QSemaphore in Qt4, maybe it would fit...
    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.

    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.

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.