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

Thread: QT app Run once

  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default QT app Run once

    Is there a way to prevent a QT app to be executed twice ?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT app Run once

    If you are a commercial user and willing to pay a few bucks more, then QtSingleApplication is for you .

  3. #3
    Join Date
    Jan 2008
    Location
    Warsaw, Poland
    Posts
    26
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    2

    Default Re: QT app Run once

    if you are not a commercial user, then you have few choices.
    On *NIX often used approach is creating a lock file (Unix Daemon Server Programming), on Windows - creating a mutex (MSDN: Mutex Objects).

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT app Run once

    There are better ways on Windows than with an interprocess mutex.
    One would be: "On Windows the implementation uses FindWindow() to detect a running instance, and the WM_COPYDATA message to send string data." This is from the QtSingleApplication documentation. One could also use lock files on windows too.

  5. #5
    Join Date
    Jan 2008
    Location
    Warsaw, Poland
    Posts
    26
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    2

    Default Re: QT app Run once

    Quote Originally Posted by marcel View Post
    There are better ways on Windows than with an interprocess mutex.
    I didn't say that mutexes are the best.
    Quote Originally Posted by marcel View Post
    One could also use lock files on windows too.
    Of course, and mutexes (or semaphores, depends on chosen "backend" - POSIX or not) on unix. There are also named pipes, shared memory (without mutexes we can get races, but I bet there exists a program using only shm to achieve only one instance running, in most cases it'll work...) and others that I not remember right now.

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT app Run once

    I only wanted to point the solution used by the QtSingleApplication.
    I am sure they opted for the most efficient solution when they implemented that class.

    Of course, and mutexes (or semaphores, depends on chosen "backend" - POSIX or not) on unix. There are also named pipes, shared memory (without mutexes we can get races, but I bet there exists a program using only shm to achieve only one instance running, in most cases it'll work...) and others that I not remember right now.
    There are many methods, I can't argue with that. But some of them are almost "theoretical". There's no winning in implementing them.
    On Unix they use: "On X11 systems the implementation uses X atoms to communicate through the selection mechanism.".

  7. #7
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QT app Run once

    Can't believe you have to pay additional fees for such a class.

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT app Run once

    Why don't you create it yourself?
    You have all the necessary information...

  9. #9
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QT app Run once

    Well that's why you use Qt over MFC, because you don't have to code architecture specific components. If one purchase a Qt license he could expect to have this crucial feature out of the box.

    Correct me if I'm wrong but QtSinglaApp feels more like : "Too bad you need to pay extra bucks for doing that crucial feature".
    Instead of
    "It's so complicated to develop we've decided to charge you more".

    Anyway, thanks for the help guys .

  10. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT app Run once

    If one purchase a Qt license he could expect to have this crucial feature out of the box.
    It is not a crucial feature. You just feel like that because you need it right now.
    If you'd have started to implement your own last night when you first asked about it, it would have been ready right now. Twice .

  11. #11
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QT app Run once

    Quote Originally Posted by marcel View Post
    It is not a crucial feature. You just feel like that because you need it right now.
    If you'd have started to implement your own last night when you first asked about it, it would have been ready right now. Twice .
    I guess that's the difference between you and me marcel. I'm taking one week to develop such a feature :-).

  12. #12
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT app Run once

    Quote Originally Posted by bunjee View Post
    I guess that's the difference between you and me marcel. I'm taking one week to develop such a feature :-).
    But why? It shouldn't. I find it very useful that Trolltech offered some insight on how QtSingleApplication was implemented. Once you had this information, the Internet is full of documentation on FindWindow and WM_COPYDATA, and the other methods used on the other platforms.

  13. The following user says thank you to marcel for this useful post:

    bunjee (13th February 2008)

  14. #13
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QT app Run once

    Right,
    I'll take a look and I'll post my home-made implementation.

    And guess what ? It'll be free.

  15. #14
    Join Date
    Mar 2007
    Location
    Ukraine, Odessa
    Posts
    140
    Thanks
    15
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QT app Run once

    Anybody knows where can I get first parameter for FindWindow function? It is registered window class name and I don't know how to obtain it
    C++ & AMD forever

  16. #15
    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: QT app Run once

    Just as docs say, use RegisterClass(). If title is enough, you can do:
    Qt Code:
    1. WId QxtWindowSystem::findWindow(const QString& title)
    2. {
    3. std::wstring str = title.toStdWString();
    4. return ::FindWindow(NULL, str.c_str());
    5. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  17. #16
    Join Date
    Mar 2007
    Location
    Ukraine, Odessa
    Posts
    140
    Thanks
    15
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QT app Run once

    It's funny today I've asked two questions and 5 minutes after I've found a solution myself
    But I suppose that hardcoding window name is not the best idea...
    I've done it like this
    Qt Code:
    1. wchar_t *str = new wchar_t[16];
    2. mbstowcs(str,"My Window Title", 16 );
    3. qDebug()<<"------------------------------------";
    4. qDebug()<<FindWindow(0, str);
    5. qDebug()<<"------------------------------------";
    To copy to clipboard, switch view to plain text mode 
    But anyway thanks jpn
    C++ & AMD forever

  18. #17
    Join Date
    Mar 2007
    Location
    Ukraine, Odessa
    Posts
    140
    Thanks
    15
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QT app Run once

    Does anybody know how to implement run once application on Mac OS X ? In the docs on QSingleApplication I've read that they use "named ports". Anyone knows what is it ? Some links would be very useful...
    C++ & AMD forever

  19. #18
    Join Date
    Aug 2006
    Posts
    90
    Thanks
    6
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT app Run once

    Yeah, I needed that class... but then I found out that even though I have a commercial developer license I needed to buy a special license just to get it. So to make a long story short... I spent 5 minutes and I wrote my own. I have my own cross platform mutex/semaphore class and I used it.

    Here is a quick example of how it works using semaphore.h... as if you didn't know.
    Qt Code:
    1. // Example using <semaphore.h> under linux.
    2. string sSemaphoreKey = "test777";
    3.  
    4. cout << "Opening semaphore: " << sSemaphoreKey << endl;
    5.  
    6. sem_t* mySemaphore = sem_open(sSemaphoreKey.c_str(), O_CREAT|O_EXCL, S_IRUSR|S_IRGRP|S_IROTH, 1);
    7.  
    8. if (mySemaphore == SEM_FAILED)
    9. {
    10. cout << "Failed to open semaphore: " << sSemaphoreKey << endl;
    11. return 1;
    12. }
    To copy to clipboard, switch view to plain text mode 

    Anyone thinking about buying that crappy class should light a 100$ bill on fire so they can learn before hand what it is like to burn money.
    Last edited by bpetty; 4th March 2008 at 21:00.

  20. #19
    Join Date
    Feb 2008
    Posts
    153
    Thanks
    40
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Talking Re: QT app Run once

    Quote Originally Posted by bpetty View Post
    Anyone thinking about buying that crappy class should light a 100$ bill on fire so they can learn before hand what it is like to burn money.
    Hmm, not bad, but what's sem_open? Nvm, I'll figure something out and post here.
    Last edited by codeslicer; 5th March 2008 at 00:32.

  21. #20
    Join Date
    Feb 2008
    Posts
    153
    Thanks
    40
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT app Run once

    I was going to make my own code, but then I decided to look around for some open source apps which already implemented this. Then I found the infamous QGRUBEditor (now known as KGRUBEditor). I looked in the source code and I found this in main.cpp:

    Qt Code:
    1. QFile lockFile( QDir::tempPath() + "/qgrubeditor.lock" );
    2. if ( lockFile.open( QIODevice::ReadOnly ) )
    3. {
    4. QTextStream lockStream( &lockFile );
    5. if ( QDir( "/proc/" + lockStream.readLine() ).exists() )
    6. {
    7. qWarning( "QGRUBEditor: Already running" );
    8. return 0;
    9. }
    10. lockFile.close();
    11. }
    12. if ( lockFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
    13. {
    14. QTextStream lockStream( &lockFile );
    15. lockStream << getpid() << endl;
    16. lockFile.close();
    17. }
    To copy to clipboard, switch view to plain text mode 

    Now, this code isn't very system independent. To make it system-independent, we /proc/ and getpid() has to be eliminated. So, how would I find out an "id" of the application?

    Or, is there an alternative way? Using sockets seems wrong... it looks like it can cause problems for other applications.

    This is another idea: What if I create, in the event loop, a function which will constantly update the lockfile with the current time, and then have a new instance of the application check if the time of the file is different from the current file. This will use too much hard drive though...
    Locking a file seems a good idea, and the Qxt extension contains a file locking class. Perhaps I could import some of the more vital functions from there? Also, is the file unlocked when the process is terminated?


    If there is no system-independent way, how would I achieve this? I already have the code for Linux, but I don't have a Mac to test the code on nor do I know of any way to check it on Windows. Semaphores - do they work in between applications?

    Well... if anyone has any bright ideas, please make a new thread or reply here. Thanks!

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.