PDA

View Full Version : How to not start the application second time if it is already running?



slava
9th March 2008, 11:13
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?

momesana
9th March 2008, 12:07
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-programming-2/t-qt-app-run-once-11824.html/?highlight=QtSingleApplication

slava
9th March 2008, 13:48
I didn't find any real solution... thanks anyway

wysota
9th March 2008, 15:22
Really? Did you try searching for "QtSingleApplication"?

slava
9th March 2008, 17:28
I looked through all that. I like the idea of "lockFile" but somehow on my WinXP planform I do:



if (QFile ("lockFile.lock").isOpen())
{
QMessageBox::about(0, "Attention". "The app is already running"
...
}
QFile lockFile("lockFile.lock");
lockFile.open(QFile::WriteOnly);
...


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.

wysota
9th March 2008, 17:37
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.

slava
9th March 2008, 19:29
Sorry, how do I do that, put

private:
QFile lockFile;

in the header file or I'm completely missing it

slava
9th March 2008, 20:02
Thank you I have found it. I just put it before the window::window() part. That is working perfectly now. Thank you very much!

slava
9th March 2008, 20:42
.... just a last question. When I do:



if (QFile ("lockFile.lock").isOpen())
{
QMessageBox::about(0, "Attention". "The app is already running");
close();
}

QFile lockFile("lockFile.lock");
lockFile.open(QFile::WriteOnly);
...

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?

momesana
10th March 2008, 10:28
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:



if (!lockFile.open(QFile::WriteOnly)) {
QMessageBox::warning(...);
return;
}

slava
10th March 2008, 12:28
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.