Solved Qt5 prevent multiple instances of application
-
Hello,
I have a program I developed with Qt5 under Windows10. I would like to prevent the case where multiple instances of the application might run. Only one instance of the program should run.
I read that it was possible to do with QtSingleApplication (which is an external package not included in Qt) but that it was not possible under Qt5. https://forum.qt.io/topic/17785/solved-how-to-prevent-multiple-instances-of-application
Thank you very much for your help,
Alex
-
@alecs26 ,
Inside the main(), put this
QApplication a(argc, argv);QSharedMemory shared("62d60669-bb94-4a94-88bb-b964890a7e04");
if( !shared.create( 512, QSharedMemory::ReadWrite) )
{
qWarning() << "Can't start more than one instance of the application.";
exit(0);
}
else {
qDebug() << "Application started successfully.";
} -
@Vinod-Kuntoji Wow, that's nice, thank you so much for you fast answer !
Alex -
Hi,
Don't get me wrong but you are basing your assumption on a 5 years old answer where it's nowhere stated that it's not working with Qt 5. In fact, Qt 5 is not discussed at all on that thread and the original poster found out a bug in its own application which was not related to that module.
Now back to your problem: QtSingleApplication works with Qt 5.
One of the advantages of QtSingleApplication is that you can more easily communicate with the original instance of the application.
-
@Vinod-Kuntoji said in Qt5 prevent multiple instances of application:
@alecs26 ,
Inside the main(), put this
QApplication a(argc, argv);QSharedMemory shared("62d60669-bb94-4a94-88bb-b964890a7e04");
I assume that is supposed to be a unique GUID/identifier per application? The trouble is, once you post this as the answer, any number of readers may copy it into any number of applications, and then they each block one another.... Assuming that is correct, I think you ought tell the OP/others that a distinct string must be generated per each application, don't you think?
See instead https://stackoverflow.com/a/33526851/489865, for an implementation of this principle which does not "clash" with others.
Otherwise @SGaist's
QtSingleApplication
may be the way to go. -
@JNBarchan said in Qt5 prevent multiple instances of application:
The trouble is, once you post this as the answer, any number of readers may copy it into any number of applications, and then they each block one another....
i think the chances that this happens (on the same machine!) are very very small. ;)
And (personally) i doubt that an application of a developer which doesn't see/know that it's supposed to be unique is spread like this :)Nevertheless of course you are basically right.
-
@raven-worx said in Qt5 prevent multiple instances of application:
@JNBarchan said in Qt5 prevent multiple instances of application:
The trouble is, once you post this as the answer, any number of readers may copy it into any number of applications, and then they each block one another....
i think the chances that this happens (on the same machine!) are very very small. ;)
If I copy the code suggestion into my application, and the OP copies it into his application, the two applications cannot be run at the same time on any machine!
-
@JNBarchan
so you think a shared memory is shared world-wide to any machine??? o.O
are you really serious?!? -
@raven-worx said in Qt5 prevent multiple instances of application:
@JNBarchan
so you think a shared memory is shared world-wide to any machine??? o.O
are you really serious?!?Oh, I think we are misunderstanding. When I wrote
the two applications cannot be run at the same time on any machine
I meant, the two apps cannot both be run on the same machine anywhere! Not, each one on a different machine, obviously!
The point is not really that the OP should not use that literal string --- though I'd really rather he did not --- it's just that if the post suggestion simply alerted the reader to the fact that the string should be unique across applications we would know where we are. Hence I drew attention to the fact. That's all.
-
@Vinod-Kuntoji Thank you very, very, very much.
-
This is a very old post but I want to share my experience for newcomers.I decided to use @Vinod-Kuntoji solution and it worked as expected. However, when testing the application on a linuxenvironment I ran into a problem: if my application crashes or finishes unexpectedly, when trying to run it again I alwaysreceived the message stating that the application is already running, mostly when rerunning the app from the Qt Creator thatwould kill the running app and run it again. It was pretty annoying, I had to change the ID string when that happened.I got past the problem by doing this:int main(int argc, char *argv[]) { /* QApplication a(argc, argv); QSharedMemory* shared = new QSharedMemory("62d60yyiassassasaasxss669-bb94-4a9gjhsfgjdhiuj4-8ihi8bb-b9648kjuiujhjhtgjdf90a7e04"); shared->deleteLater(); // this solved the problem if( ! shared->create( 512, QSharedMemory::ReadWrite) ) { QMessageBox errorMessage; QApplication::beep(); errorMessage.addButton(QMessageBox::Ok); errorMessage.setWindowTitle(QObject::tr("Error")); errorMessage.setText(QObject::tr("Already running")); errorMessage.setInformativeText(QObject::tr("More than one instance of the program is not permitted")); errorMessage.exec(); exit(0); } else { MainWindow w; w.show(); return a.exec(); } */ }
-
@hbatalha said in Qt5 prevent multiple instances of application:
shared->deleteLater(); // this solved the problem
Are you sure this did not solve the problem by releasing the shared memory during
a.exec()
? You verified another instance still doesn't run? -
@JonB I tested one way only, it actually defeated the purpose.
-
@JonB I believe this answer solves the problem I described above.