Results 1 to 11 of 11

Thread: How to know if your app is already running when you start it.

  1. #1
    Join Date
    Dec 2007
    Posts
    3
    Thanked 1 Time in 1 Post

    Default How to know if your app is already running when you start it.

    Hello everyone,

    Would somebody know some way of see if your app is already running when you start it and therefore stop the launching?

    I work with Qt4.3.2 on XP Sp2.

    Thanks a lot ...

  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: How to know if your app is already running when you start it.

    There's a commercial solution for this, from Trolltech: http://trolltech.com/products/qt/add...leapplication/.

    Otherwise, you have to find platform dependent solutions. For windows you can search codeproject.com and for Mac there are probably solutions on developer.apple.com.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to know if your app is already running when you start it.

    linux apps often solve this problem by creating a lock file containing the process id of the running application. If another instance of the app is launched it will check if such a lock file exists. if not everything is ok and the app starts and creates its own lock file. If there already exists a pid lock file, the new app extracts the process id from the file and checks whether the app is still running (the pid is still valid). If it is the app will exit, if not the app will start and overwrite the lock file with its own process id. The application should delete their corresponding pid on exit. If it crashes and thus cannot delete the lockfile, this approach still works due to the checking of the state of the pid.

  4. #4
    Join Date
    Dec 2007
    Posts
    3
    Thanked 1 Time in 1 Post

    Thumbs up Re: How to know if your app is already running when you start it.

    The "lock file" runs great

    I was thinking doing something like that but with a socket instead. I think with a file is better.

    If somebody is interested I,ve done this in the entry point of the app:


    Qt Code:
    1. int main(int argc, char *argv[]) {
    2.  
    3. Csec csec(argc, argv);
    4.  
    5. QFile lockFile("lock.dat");
    6. if (lockFile.exists() && !lockFile.remove()) {
    7. QMessageBox::critical("BLA BLA BLA");
    8. return 0;
    9. }
    10. if (!lockFile.open(QIODevice::Truncate | QIODevice::WriteOnly)) {
    11. QMessageBox::critical("BLA BLA BLA");
    12. return 0;
    13. }
    14.  
    15. MainWindow *mainWindow = new MainWindow;
    16. mainWindow->show();
    17. int rtn = csec.exec();
    18.  
    19. lockFile.close();
    20.  
    21. return rtn;
    22. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 16th December 2007 at 18:15. Reason: missing [code] tags

  5. The following user says thank you to simper66 for this useful post:

    hvw59601 (17th December 2007)

  6. #5
    Join Date
    Nov 2007
    Location
    Russia, Moscow
    Posts
    21
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to know if your app is already running when you start it.

    Quote Originally Posted by simper66 View Post
    QFile lockFile("lock.dat");
    Just a note. The better way is to use absolute path to the file. Your app will create a "lock.dat" in the current directory and thus it may be started multiple times from different shourtcuts for example

  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: How to know if your app is already running when you start it.

    With this approach if your application crashes or is killed, you won't be able to restart it. You forgot to implement the second part - checking the pid contained in the file.

  8. #7
    Join Date
    Nov 2007
    Location
    Russia, Moscow
    Posts
    21
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to know if your app is already running when you start it.

    Quote Originally Posted by wysota View Post
    With this approach if your application crashes or is killed, you won't be able to restart it. You forgot to implement the second part - checking the pid contained in the file.
    This case is covered in
    Qt Code:
    1. if (lockFile.exists() && !lockFile.remove())
    To copy to clipboard, switch view to plain text mode 
    If the one app is running the second app won't be able to remove the lock.
    I don't know if holding the open file handler while the app is running is good style but it seems OK for me.

  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: How to know if your app is already running when you start it.

    I don't think this is a foulproof approach... For instance under Windows if an application holding a file open crashes, files opened with mandatory locking (which seems to be the default case on Windows when opening for writing) can't be opened again (or removed) by another process. This is a tricky situation... The best possible solution is to use shared memory or a similar ipc mechanism (sockets are fine too although they trash the system a bit).

  10. #9
    Join Date
    Dec 2007
    Posts
    3
    Thanked 1 Time in 1 Post

    Default Re: How to know if your app is already running when you start it.

    Well, after some time playing with it the method is running OK everytime.

    The better way is to use absolute path.................. from different shourtcuts for example
    A good appreciation.


    ....if your application crashes or is killed, you won't be able to restart it.....
    I make that test. I aborted the program before close the file and the app could be restarted later. I suppose that the SO itself close the file if the PID is dead.
    Anyway, you can know what is your PID with the QProcess:id() method, but how can you discover what other processes are running in the system?????? I can´t find this functionality anywhere.


    Another two observations:

    - I edited the piece of code because the line Csec csec(argc, argv); has to be the first line. Otherwise the first "alert" don´t run. I suppose this is because the event loop must be running for dialogs to run.

    - In fact, the only one sentence that really do all the work is this:
    if (lockFile.exists() && !lockFile.remove())
    because if i start the app lots of times this sentence always goes well
    if (!lockFile.open(QIODevice::Truncate | QIODevice::WriteOnly))
    It seems like lots of processes could open a file in write mode, but fortunately they can´t
    delete it if it´s opened by another one.
    Last edited by simper66; 16th December 2007 at 18:42.

  11. #10
    Join Date
    Jul 2007
    Location
    Jundiai/SP, Brazil
    Posts
    114
    Thanks
    5
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to know if your app is already running when you start it.

    Hi,

    This is my code (which is similar to the above) and works fine:

    In the main.cpp program:

    Qt Code:
    1. QString arq;
    2. #ifdef Q_OS_WIN32
    3. arq="C:\\windows\\system32\\lock.ctl";
    4. #else
    5. arq="/var/run/lock.ctl";
    6. #endif
    7. QFile dlock(arq);
    8. if(dlock.exists()) {
    9. QMessageBox::critical(mw,"MyApp",
    10. ".this programs already is running",
    11. QMessageBox::Close);
    12. QApplication::restoreOverrideCursor();
    13. return -1;
    14. }
    15. else {
    16. dlock.open(QFile::WriteOnly | QFile::Truncate);
    17. QTextStream out(&dlock);
    18. out << "running" << endl;
    19. dlock.close();
    20. }
    21.  
    22.  
    23. mw->show();
    24. return app.exec();
    To copy to clipboard, switch view to plain text mode 


    In the MainWindow subclass, make follow:

    Create a eventClose(QEventClose *event) protected funcion like this.
    This funcion will be remove the lock file when the user end the application.

    Qt Code:
    1. void Compras::closeEvent(QCloseEvent *event)
    2. {
    3. if(!isWindowsActived() ) {
    4. QString arq;
    5. #ifdef Q_OS_WIN32
    6. arq="qcompras.ctl";
    7. QDir d("C:\\windows\\system32\\");
    8. #else
    9. arq="lock.ctl";
    10. QDir d("/var/run/");
    11. #endif
    12. d.remove(arq);
    13. event->accept();
    14. close();
    15. }
    16. else
    17. event->ignore();
    18. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 17th December 2007 at 12:58. Reason: missing [code] tags

  12. #11
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to know if your app is already running when you start it.

    I'am use QtSingleApplication
    http://doc.trolltech.com/solutions/4...plication.html

    Sample:

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. //QApplication::setStyle (new QPlastiqueStyle());
    4. //QApplication a(argc, argv);
    5. QtSingleApplication::setStyle(new QPlastiqueStyle());
    6. QtSingleApplication a("iTMHSM",argc, argv);
    7.  
    8. if (a.sendMessage("12345"))
    9. return 0;
    10.  
    11. a.initialize();
    12.  
    13. HSM w;
    14. w.show();
    15.  
    16. QObject::connect(&a, SIGNAL(messageReceived(const QString&)),
    17. &w, SLOT(once(const QString&)));
    18. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    19.  
    20. return a.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. running() - Thread
    By sabeesh in forum Qt Programming
    Replies: 5
    Last Post: 9th October 2007, 19:45
  2. Questions about kill() and start() of QProcess
    By mp33919 in forum Qt Programming
    Replies: 5
    Last Post: 23rd June 2007, 14:00
  3. Replies: 10
    Last Post: 11th June 2007, 10:18
  4. QThread: Destroyed while thread is still running
    By Shuchi Agrawal in forum Newbie
    Replies: 8
    Last Post: 3rd April 2007, 07:27
  5. Qt interface running extremely slowly...
    By jazztpt in forum Qt Programming
    Replies: 1
    Last Post: 4th February 2006, 12:12

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.