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. Qt single instance app how to Manage on crash
QtWS25 Last Chance

Qt single instance app how to Manage on crash

Scheduled Pinned Locked Moved General and Desktop
13 Posts 6 Posters 3.4k Views
  • 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.
  • H Offline
    H Offline
    haris123
    wrote on last edited by
    #1

    Hi,

    I want to execute my application as single instance, currently I am using QSharedMemory and working fine. I am using Qt5.2.1 on Ubuntu 12.04. below is my test code,

    @ QApplication a(argc, argv);
    a.processEvents();

    const char* MEM_KEY = "56";
    QSharedMemory sharedMem(MEM_KEY);
    
    if( !sharedMem.create( 512, QSharedMemory::ReadWrite) )
    {
      QMessageBox msgBox;
      msgBox.setText( QObject::tr("Can't start more than one instance of the application.") );
      msgBox.setIcon( QMessageBox::Critical );
      msgBox.exec();
      exit(0);
    }
    
    
    MainWindow w;
    w.show();
    
    int p=0;
    //p=p/0; // create exception here
    
    return a.exec();@
    

    But the if I made the application crash(as shown in above code) and start the application again it showing @Can't start more than one instance of the application@ means the previous instance is still there even if it's crashed. It should not happened in my case, how can I restart my application in such a situation?.

    Thanks
    Haris

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Try using QtSingleApplication: "link":https://qt.gitorious.org/qt-solutions/qt-solutions/source/38e79e3f04d6fd5a3df585a60b2aec95e8e68368:qtsingleapplication

      It is not part of Qt, but is created an maintained by Qt Company, so it's very close.

      (Z(:^

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Francknos
        wrote on last edited by
        #3

        Or you can do like me.
        Check if .exe is running ... :

        @
        //in main => MainWidget m; m.show();
        MainWidget::MainWidget(int argc, char** argv, QWidget *parent) :
        QWidget(parent),
        ui(new Ui::MainWidget)
        {
        initialisations();

        #ifdef ONLY_ONE_INSTANCE
        checkOnlyOneInstance();
        #endif
        }
        @
        checkOnlyOneInstance
        @
        void MainWidget::checkOnlyOneInstance()
        {
        QProcess x;
        x.start("tasklist");
        x.waitForStarted(200); //1000 avant
        x.waitForReadyRead(200);
        x.waitForFinished(200);
        QString result(x.readAllStandardOutput());
        if(result.count("MON APPLICATION.exe",Qt::CaseInsensitive) > 1 )
        {
        popupMessage(tr("L'Application est déjà en cours d'execution!"),2000,this);
        QTimer::singleShot(2000, qApp, SLOT(quit()));
        }
        }
        @

        It works for me.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Beware, you are doing your initialization twice. You're application should not start any initialization before checking it's not already running. Also that's a Windows specific implementation and the OP is running Ubuntu. sierdzio's suggestion is good. It works very well.

          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
          • F Offline
            F Offline
            Francknos
            wrote on last edited by
            #5

            My Initialisation is just to config the GUI ... (stylesheet ...)

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              [quote author="Francknos" date="1424334416"]My Initialisation is just to config the GUI ... (stylesheet ...)[/quote]

              Yes, but it does start the application. So, for a tiny bit of time, you do have 2 instances of your application launched.

              (Z(:^

              1 Reply Last reply
              0
              • F Offline
                F Offline
                Francknos
                wrote on last edited by
                #7

                Yes I have 2 instances of my aplpication during 2000 ms.
                But it's not a problem for me.

                I can check this in main so ...

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  Francknos
                  wrote on last edited by
                  #8

                  [quote author="SGaist" date="1424300270"]Hi,

                  Beware, you are doing your initialization twice. You're application should not start any initialization before checking it's not already running. Also that's a Windows specific implementation and the OP is running Ubuntu. sierdzio's suggestion is good. It works very well.[/quote]

                  I have tried this but it doesn't work with me.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    What doesn't work ?

                    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
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #10

                      You could also use D-Bus in stead of shared memory.

                      1 Reply Last reply
                      0
                      • jerome_isAviableJ Offline
                        jerome_isAviableJ Offline
                        jerome_isAviable
                        wrote on last edited by
                        #11

                        you also can try to create and use a specific singleton class.
                        look at C++ codes/exemples for learn how to use well singleton class.
                        i do it for check login and users access/privileges.
                        after that, you can call your singleton by handle the "get_instance" method (for exemple).

                        1 Reply Last reply
                        0
                        • sierdzioS Offline
                          sierdzioS Offline
                          sierdzio
                          Moderators
                          wrote on last edited by
                          #12

                          [quote author="jerome_isAviable?" date="1424746717"]you also can try to create and use a specific singleton class.[/quote]

                          That won't work for this use case. Singleton has a single instance of a class per application - but there can be many applications launched. The OP wants to run only one instance of the application.

                          (Z(:^

                          1 Reply Last reply
                          0
                          • jerome_isAviableJ Offline
                            jerome_isAviableJ Offline
                            jerome_isAviable
                            wrote on last edited by
                            #13

                            exact.

                            1 Reply Last reply
                            0

                            • Login

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