Results 1 to 14 of 14

Thread: Multiple program instances

  1. #1
    Join Date
    Feb 2007
    Posts
    29
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Multiple program instances

    For a lot of programs, such as Gaim, when you run the binary if there is already a instance of the program open currently opened program will execute some kind of command and a new task will not be started.

    Does Qt support a cross platform way to do this or do I need to use some kind of platform specific function to read the process list (or something to that extent).

  2. #2
    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: Multiple program instances

    There exists a QtSolution which does what you want in a portable way. If you have a commercial licence with QtSolution subscription, you can make use of it. If not, you'll have to implement it yourself in a platform dependent way.

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

    scwizard (1st April 2007)

  4. #3
    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: Multiple program instances

    Quote Originally Posted by wysota View Post
    If not, you'll have to implement it yourself in a platform dependent way.
    Alternatively you can use the GPL'ed solution I've myself developped for Edyuk which provide similar functionnality in a platform independent way through local networking (i.e. TCP sockets/server on 127.0.0.1). It is available in the Edyuk source packages from version 0.8.0-rc1 or in the SVN (recommeded) :
    Qt Code:
    1. $ svn co https://edyuk.svn.sourceforge.net/svnroot/edyuk/3rdparty/qcumber qcumber
    To copy to clipboard, switch view to plain text mode 
    Note : QCumber is the name of a multi-purpose library meant to bring features frequently used by many apps (instance control and communication, shortcut management, safe shared settings, ...). It is still under active development but wht's available works pretty well.

    If you need an example there's one bundled which demonstrates the power and flexibility of my solution.

    Hope this helps.
    Current Qt projects : QCodeEdit, RotiDeCode

  5. #4
    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: Multiple program instances

    To be honest now I'd expect a singleton app to be handled on Unix through dbus and not a network socket Using QSettings or shared memory is also a way...

  6. #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: Multiple program instances

    Quote Originally Posted by wysota View Post
    To be honest now I'd expect a singleton app to be handled on Unix through dbus and not a network socket Using QSettings or shared memory is also a way...
    Well...
    1. I don't know how D-Bus works
    2. D-Bus is not cross-platform
    3. QSettings is also used as a part of my solution
    4. Without network cross-platform *communication* is a mess
    5. How do you use shared memory safely between two applications/instances of an application?
    Do you understand my choice a little better?
    Current Qt projects : QCodeEdit, RotiDeCode

  7. #6
    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: Multiple program instances

    Quote Originally Posted by fullmetalcoder View Post
    I don't know how D-Bus works
    You don't have to know how it works to use it.
    D-Bus is not cross-platform
    Yes, but who said all platforms should use the same solution?
    Without network cross-platform *communication* is a mess
    I don't know if I understand... Where is the "cross-platforminess" in talking between two instances of a program running on the same computer?
    How do you use shared memory safely between two applications/instances of an application?
    Just like any application using shared memory does it... I don't know what is so unsafely in using shared memory...

    Do you understand my choice a little better?
    Honestly? No. The main problem I see is that you can have clashes between two completely different applications that by accident use the same port for their "singleton" sockets. I must admit I didn't read your code, that's why I'm having these doubts.

  8. #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: Multiple program instances

    Quote Originally Posted by wysota View Post
    You don't have to know how it works to use it.
    I was talking about the API...

    Quote Originally Posted by wysota View Post
    Yes, but who said all platforms should use the same solution?
    Me! Because it's easier to write and maintain.... Besides it allows real platform independence without bringing extra dependencies. Another interest is that deadlocks are very unlikely to occur and easy to overcome. For instance if your want to use system mutex (under windows or linux) to handle instance limiting you will face two problems :
    1. If the app crashes before releasing the mutex, only a reboot or a tricky low-level manip can allow the user to restart it
    2. what if you want you app to be configurable? i.e. the choice of single/multiple instances would be up to the user...
    Quote Originally Posted by wysota View Post
    I don't know if I understand... Where is the "cross-platforminess" in talking between two instances of a program running on the same computer?
    In having the same easy to maintain implementation for all platforms which make the classes compatible with ANY platform supported by Qt4...

    Quote Originally Posted by wysota View Post
    Just like any application using shared memory does it... I don't know what is so unsafely in using shared memory...
    I've never used shared memory so I probably can't tell you... As you seem to known it maybe you can tell how easy to use shared memory is... Anyway I don't really see the point of shared memory here... Would you like to use it as a message bus? If so how? And how do you effectively share this memory exclusively with other instances of your app?

    Quote Originally Posted by wysota View Post
    Honestly? No. The main problem I see is that you can have clashes between two completely different applications that by accident use the same port for their "singleton" sockets. I must admit I didn't read your code, that's why I'm having these doubts.
    This is very unlikely to happen but can anyway be easily fixed, if faced, by prepending a pre-application ID string to the messages sent...
    Current Qt projects : QCodeEdit, RotiDeCode

  9. #8
    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: Multiple program instances

    I'll just answer that part about shared memory. It's called shared because different processes can have it in their memory space simoultaneously. Checking if an application instance exists is just about checking if a particular shared memory segment exists and maybe also what it contains.

    Another possiblity is to use named pipes or message queues.

    BTW. Are you using TCP or UDP sockets and why? How do you bind to an address? What happens if another application (I mean one that actually uses the socket for something) binds the socket exclusively?

  10. #9
    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: Multiple program instances

    Quote Originally Posted by wysota View Post
    Another possiblity is to use named pipes or message queues.
    Is this platform-independent, easy to use and "dependence-free" (apart from Qt of course)???

    Quote Originally Posted by wysota View Post
    BTW. Are you using TCP or UDP sockets and why? How do you bind to an address? What happens if another application (I mean one that actually uses the socket for something) binds the socket exclusively?
    I'm using TCP sockets because they can't be taken over exclusively... Besides it is highly unprobable that an application uses the local host to "actually do something"...
    Current Qt projects : QCodeEdit, RotiDeCode

  11. #10
    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: Multiple program instances

    Quote Originally Posted by fullmetalcoder View Post
    Is this platform-independent, easy to use and "dependence-free" (apart from Qt of course)???
    Dependence-free - yes, easy to use - yes, platform independent - depends what you mean Named pipes exist on most (all?) modern systems, but the API probably differs.


    I'm using TCP sockets because they can't be taken over exclusively...
    Hmm? Have you tried running two instances of any TCP server on the same port?

    Besides it is highly unprobable that an application uses the local host to "actually do something"...
    It's enough if it binds to 0.0.0.0 before your application binds to 127.0.0.1.

    What about using UDP? It's lightweight, reliable when used on loopback device and datagram oriented, so it should be easier to handle. It's also connectionless, so many apps can use it and it's less probable to cause conflicts with other applications.

  12. #11
    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: Multiple program instances

    Quote Originally Posted by wysota View Post
    What about using UDP? It's lightweight, reliable when used on loopback device and datagram oriented, so it should be easier to handle. It's also connectionless, so many apps can use it and it's less probable to cause conflicts with other applications.
    I've tried using UDP but I did not come to any decent result. Besides, what I've read in the docs makes me think that it's the exact contrary of what I want in term of listenning mechanism... Actually I'm not quite sure I fully understood the restrictions (differences) UDP brings but my tests appeared to be in favour of TCP... Maybe you can show me how wrong I was just by providing a UDP based implementation of the QInterProcessChannel class...
    Current Qt projects : QCodeEdit, RotiDeCode

  13. #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: Multiple program instances

    Quote Originally Posted by fullmetalcoder View Post
    Maybe you can show me how wrong I was just by providing a UDP based implementation of the QInterProcessChannel class...
    Sorry, no time for that now Any app that is longer than 20 LOC is off limits for me now

  14. #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: Multiple program instances

    Quote Originally Posted by wysota View Post
    Sorry, no time for that now Any app that is longer than 20 LOC is off limits for me now
    Then we'll stick to the TCP implementation until your LOC limits grows a bit...
    Current Qt projects : QCodeEdit, RotiDeCode

  15. #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: Multiple program instances

    I don't say your implementation is bad. It just could be better

Similar Threads

  1. Porting my program to another windows machine !
    By probine in forum Qt Programming
    Replies: 1
    Last Post: 14th March 2007, 06:46
  2. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 04:19
  3. how to corss compile for windows in Linux
    By safknw in forum Qt Programming
    Replies: 24
    Last Post: 13th May 2006, 05:23
  4. Enter key causing program to exit
    By welby in forum Qt Programming
    Replies: 2
    Last Post: 9th March 2006, 16:11
  5. Replies: 25
    Last Post: 15th January 2006, 00:53

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.