Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt5 prevent multiple instances of application
Forum Updated to NodeBB v4.3 + New Features

Qt5 prevent multiple instances of application

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 7 Posters 8.5k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alecs26
    wrote on 15 Sept 2017, 11:59 last edited by
    #1

    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

    1 Reply Last reply
    0
    • V Offline
      V Offline
      Vinod Kuntoji
      wrote on 15 Sept 2017, 12:20 last edited by
      #2

      @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.";
      }

      C++, Qt, Qt Quick Developer,
      PthinkS, Bangalore

      J B 2 Replies Last reply 15 Sept 2017, 13:16
      1
      • A Offline
        A Offline
        alecs26
        wrote on 15 Sept 2017, 12:32 last edited by
        #3

        @Vinod-Kuntoji Wow, that's nice, thank you so much for you fast answer !
        Alex

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 15 Sept 2017, 12:59 last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • V Vinod Kuntoji
            15 Sept 2017, 12:20

            @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.";
            }

            J Online
            J Online
            JonB
            wrote on 15 Sept 2017, 13:16 last edited by JonB
            #5

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

            R 1 Reply Last reply 15 Sept 2017, 13:51
            2
            • J JonB
              15 Sept 2017, 13:16

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

              R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 15 Sept 2017, 13:51 last edited by raven-worx
              #6

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

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              J 1 Reply Last reply 15 Sept 2017, 14:00
              0
              • R raven-worx
                15 Sept 2017, 13:51

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

                J Online
                J Online
                JonB
                wrote on 15 Sept 2017, 14:00 last edited by JonB
                #7

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

                R 1 Reply Last reply 15 Sept 2017, 14:30
                0
                • J JonB
                  15 Sept 2017, 14:00

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

                  R Offline
                  R Offline
                  raven-worx
                  Moderators
                  wrote on 15 Sept 2017, 14:30 last edited by
                  #8

                  @JNBarchan
                  so you think a shared memory is shared world-wide to any machine??? o.O
                  are you really serious?!?

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  J 1 Reply Last reply 15 Sept 2017, 15:39
                  0
                  • R raven-worx
                    15 Sept 2017, 14:30

                    @JNBarchan
                    so you think a shared memory is shared world-wide to any machine??? o.O
                    are you really serious?!?

                    J Online
                    J Online
                    JonB
                    wrote on 15 Sept 2017, 15:39 last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • V Vinod Kuntoji
                      15 Sept 2017, 12:20

                      @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.";
                      }

                      B Offline
                      B Offline
                      Bharath Ram
                      wrote on 3 Apr 2021, 17:32 last edited by
                      #10

                      @Vinod-Kuntoji Thank you very, very, very much.

                      1 Reply Last reply
                      0
                      • H Offline
                        H Offline
                        hbatalha
                        wrote on 4 Apr 2021, 15:28 last edited by hbatalha 4 May 2021, 14:42
                        #11

                        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 linux environment I ran into a problem: if my application crashes or finishes unexpectedly, when trying to run it again I always received the message stating that the application is already running, mostly when rerunning the app from the Qt Creator that would 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();
                            }
                        */
                        }
                        J 1 Reply Last reply 4 Apr 2021, 16:24
                        0
                        • H hbatalha
                          4 Apr 2021, 15:28

                          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 linux environment I ran into a problem: if my application crashes or finishes unexpectedly, when trying to run it again I always received the message stating that the application is already running, mostly when rerunning the app from the Qt Creator that would 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();
                              }
                          */
                          }
                          J Online
                          J Online
                          JonB
                          wrote on 4 Apr 2021, 16:24 last edited by
                          #12

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

                          H 2 Replies Last reply 5 Apr 2021, 12:18
                          0
                          • J JonB
                            4 Apr 2021, 16:24

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

                            H Offline
                            H Offline
                            hbatalha
                            wrote on 5 Apr 2021, 12:18 last edited by
                            #13

                            @JonB I tested one way only, it actually defeated the purpose.

                            1 Reply Last reply
                            0
                            • J JonB
                              4 Apr 2021, 16:24

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

                              H Offline
                              H Offline
                              hbatalha
                              wrote on 5 Apr 2021, 14:44 last edited by
                              #14

                              @JonB I believe this answer solves the problem I described above.

                              1 Reply Last reply
                              2

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved