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


  • Lifetime Qt Champion

    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.


  • Moderators

    @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!


  • Moderators

    @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.