Results 1 to 11 of 11

Thread: How to not start the application second time if it is already running?

  1. #1
    Join Date
    Feb 2008
    Posts
    43
    Thanks
    14

    Question How to not start the application second time if it is already running?

    If I click on my application second time, I get two same applications running at the same time. Is there any way to for an application to check if it is running previously and do not start if it so?

  2. #2
    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 not start the application second time if it is already running?

    This has been asked many times before.
    Please search the forum first before posting questions.
    This thread might be of interest to you:
    http://www.qtcentre.org/forum/f-qt-p...gleApplication

  3. #3
    Join Date
    Feb 2008
    Posts
    43
    Thanks
    14

    Default Re: How to not start the application second time if it is already running?

    I didn't find any real solution... thanks anyway

  4. #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: How to not start the application second time if it is already running?

    Really? Did you try searching for "QtSingleApplication"?

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

    slava (9th March 2008)

  6. #5
    Join Date
    Feb 2008
    Posts
    43
    Thanks
    14

    Default Re: How to not start the application second time if it is already running?

    I looked through all that. I like the idea of "lockFile" but somehow on my WinXP planform I do:

    Qt Code:
    1. if (QFile ("lockFile.lock").isOpen())
    2. {
    3. QMessageBox::about(0, "Attention". "The app is already running"
    4. ...
    5. }
    6. QFile lockFile("lockFile.lock");
    7. lockFile.open(QFile::WriteOnly);
    8. ...
    To copy to clipboard, switch view to plain text mode 

    The first application creates the file but it does not "hold" it and the next one does not see that the file is open. I can delete or edit the file without any notification that it is used by something else.
    Last edited by jpn; 9th March 2008 at 18:46. Reason: missing [code] tags

  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 not start the application second time if it is already running?

    That's because you create the QFile object as a temporary variable so it gets destroyed immediately effectively closing the file and removing the lock.

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

    slava (9th March 2008)

  9. #7
    Join Date
    Feb 2008
    Posts
    43
    Thanks
    14

    Default Re: How to not start the application second time if it is already running?

    Sorry, how do I do that, put

    private:
    QFile lockFile;

    in the header file or I'm completely missing it

  10. #8
    Join Date
    Feb 2008
    Posts
    43
    Thanks
    14

    Default Re: How to not start the application second time if it is already running?

    Thank you I have found it. I just put it before the window::window() part. That is working perfectly now. Thank you very much!

  11. #9
    Join Date
    Feb 2008
    Posts
    43
    Thanks
    14

    Question Re: How to not start the application second time if it is already running?

    .... just a last question. When I do:

    Qt Code:
    1. if (QFile ("lockFile.lock").isOpen())
    2. {
    3. QMessageBox::about(0, "Attention". "The app is already running");
    4. close();
    5. }
    6.  
    7. QFile lockFile("lockFile.lock");
    8. lockFile.open(QFile::WriteOnly);
    9. ...
    To copy to clipboard, switch view to plain text mode 

    because of this "close();" the application crushes. It is good that it does not start second time but it produces a windows error saying that "application suddenly terminated, let's report the bug through the internet" or something. I just push ingnore and that is fine but can I do it without that terrible message from Windows?
    Last edited by jpn; 10th March 2008 at 13:30. Reason: missing [code] tags

  12. #10
    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 not start the application second time if it is already running?

    Try to wrap the stuff after the if () {} block into an else block so it is not executed at all in case the if clause is true. Maybe the stuff is executed even after close() since close() does not automatically mean an immediate exit/deletion; Also try to set the Qt::WA_DeleteOnClose attribute for your widget.

    It's also a good Idea to check if the file could be successfully opened and to return if not:

    Qt Code:
    1. if (!lockFile.open(QFile::WriteOnly)) {
    2. QMessageBox::warning(...);
    3. return;
    4. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by momesana; 10th March 2008 at 11:43.

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

    slava (10th March 2008)

  14. #11
    Join Date
    Feb 2008
    Posts
    43
    Thanks
    14

    Default Re: How to not start the application second time if it is already running?

    Thank you for the ideas. It is working now, I moved those lines to main.cpp just before the whole thing stats so it checks it earlier and returns if application is already running and that worked fine. Thank you.

Similar Threads

  1. QSkinWindows Classes
    By kernel_panic in forum Qt-based Software
    Replies: 45
    Last Post: 20th April 2010, 13:35
  2. start application: Why this error and how to solve it?
    By Colx007 in forum Qt Programming
    Replies: 1
    Last Post: 21st January 2008, 16:22
  3. Replies: 2
    Last Post: 20th November 2007, 21:00
  4. Replies: 10
    Last Post: 11th June 2007, 10:18
  5. Running application on Mac OS X
    By munna in forum General Discussion
    Replies: 1
    Last Post: 19th April 2006, 21:10

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.