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?
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
Re: How to not start the application second time if it is already running?
I didn't find any real solution... thanks anyway
Re: How to not start the application second time if it is already running?
Really? Did you try searching for "QtSingleApplication"?
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:
Code:
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.
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.
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
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!
Re: How to not start the application second time if it is already running?
.... just a last question. When I do:
Code:
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?
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:
Code:
if (!lockFile.
open(QFile::WriteOnly)) { return;
}
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.