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. Updating main QT application using updater application
Forum Updated to NodeBB v4.3 + New Features

Updating main QT application using updater application

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 835 Views 2 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.
  • artwawA artwaw

    My approach to creating autoupdater was slightly different:

    • download the update;
    • start the update QProcess as detached https://doc.qt.io/qt-6/qprocess.html#startDetached-1
    • if QProcess started succeffuly, self terminate.
      And update QProcess, should everything complete ok, would start the program anew. Can digout the code if one wishes.

    My main reasoning was - since the widnows exe can't fully manipulate itself, let it just download the files and handover update to the specified subroutine, then self terminate to let said subroutine work.

    L Offline
    L Offline
    lukutis222
    wrote on last edited by
    #5

    @artwaw This sound like exactly my approach. Why do you think mine is different?

    artwawA JonBJ 2 Replies Last reply
    0
    • L lukutis222

      @artwaw This sound like exactly my approach. Why do you think mine is different?

      artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #6

      @lukutis222

      QProcess process;
      process.start("kill", QStringList() << "-0" << QString::number(mainAppPid));
      process.waitForFinished();
      

      This?

      My approach was along the lines:

      #routines downloading and verifying update packet here 
      bool extStarted = QProcess::startDetached(params_go_here);
      #call program shutdown and exit routines here if bool is true
      

      Why would you dabble in calling QProcess to terminate self when it is safer to do so from the inside? I am leaving aside the matter of privileges required for that to succeed.

      For more information please re-read.

      Kind Regards,
      Artur

      L 1 Reply Last reply
      1
      • L lukutis222

        @artwaw This sound like exactly my approach. Why do you think mine is different?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #7

        @lukutis222
        Absolutely as @artwaw has written. Additionally what is this kill program you try to run anyway? It does not come with Windows, so if you have it at all you have added it, it won't be present for your other end users. Maybe that's why it doesn't work for you either!

        L 1 Reply Last reply
        0
        • artwawA artwaw

          @lukutis222

          QProcess process;
          process.start("kill", QStringList() << "-0" << QString::number(mainAppPid));
          process.waitForFinished();
          

          This?

          My approach was along the lines:

          #routines downloading and verifying update packet here 
          bool extStarted = QProcess::startDetached(params_go_here);
          #call program shutdown and exit routines here if bool is true
          

          Why would you dabble in calling QProcess to terminate self when it is safer to do so from the inside? I am leaving aside the matter of privileges required for that to succeed.

          L Offline
          L Offline
          lukutis222
          wrote on last edited by lukutis222
          #8

          @artwaw

          But that is exactly what I do after my failed attempt to kill the old process:

              // Kill the old application
              QProcess process;
              process.start("kill", QStringList() << "-0" << QString::number(mainAppPid));
              process.waitForFinished();
          
          
              // Restart the application
              qDebug() << "Restarting application";
              QProcess::startDetached(newExecutable);
          
          
              QCoreApplication::quit();
          

          As you can see from above, I am starting the newExecutable as new detached process which I assumed is what you mean. The only problem as I have previously mentioned is now I have 2 applications running and I want to kill the old one

          1 Reply Last reply
          0
          • JonBJ JonB

            @lukutis222
            Absolutely as @artwaw has written. Additionally what is this kill program you try to run anyway? It does not come with Windows, so if you have it at all you have added it, it won't be present for your other end users. Maybe that's why it doesn't work for you either!

            L Offline
            L Offline
            lukutis222
            wrote on last edited by lukutis222
            #9

            @JonB
            I am trying to kill old application. The process is as following:

            1. Run main QT application which starts updater.exe in detached mode but it does not end current application.
            2. Updater.exe renames the old application with TestTool_renamed.exe and places new exe as TestTool.exe
            3. Updater.exe runs the TestTool.exe as detached process
            4. Now I have 2 applications running. I want to kill the old one

            I am now looking into what is the best way to end the old application from the updater after it has finished swapping the files. Because as you mentioned, using kill is just not going to work. I wonder if I should be closing old application via the old application itlsef or from the updater as I am currently trying?

            artwawA 1 Reply Last reply
            0
            • L lukutis222

              @JonB
              I am trying to kill old application. The process is as following:

              1. Run main QT application which starts updater.exe in detached mode but it does not end current application.
              2. Updater.exe renames the old application with TestTool_renamed.exe and places new exe as TestTool.exe
              3. Updater.exe runs the TestTool.exe as detached process
              4. Now I have 2 applications running. I want to kill the old one

              I am now looking into what is the best way to end the old application from the updater after it has finished swapping the files. Because as you mentioned, using kill is just not going to work. I wonder if I should be closing old application via the old application itlsef or from the updater as I am currently trying?

              artwawA Offline
              artwawA Offline
              artwaw
              wrote on last edited by
              #10

              @lukutis222

              Always via the old application itself.
              The safest way to do so is to call close() method of the main window (which also happens to be a slot should you need it). I don't know why QCoreApplication static call doesn't work but it's not the method one would normally use anyway.

              For more information please re-read.

              Kind Regards,
              Artur

              L 1 Reply Last reply
              0
              • artwawA artwaw

                @lukutis222

                Always via the old application itself.
                The safest way to do so is to call close() method of the main window (which also happens to be a slot should you need it). I don't know why QCoreApplication static call doesn't work but it's not the method one would normally use anyway.

                L Offline
                L Offline
                lukutis222
                wrote on last edited by lukutis222
                #11

                @artwaw

                #TEST1

                In my main application, I can close the application by calling theQCoreApplication::quit() immediately after starting detached application

                    // Start the updater process
                    if (!QProcess::startDetached(updaterExecutable, QStringList() << oldExecutable << newExecutable << QString::number(QCoreApplication::applicationPid()))) {
                        qDebug() << "Failed to start updater.";
                        return;
                    }
                
                    QCoreApplication::quit();
                

                but if I do that, it does not even start updater.exe detached process which is what I am confused about. Is that expected behaviour?

                For testing purposes, I have added 5 seconds sleep before I quite main application after starting updater:

                    // Start the updater process
                    if (!QProcess::startDetached(updaterExecutable, QStringList() << oldExecutable << newExecutable << QString::number(QCoreApplication::applicationPid()))) {
                        qDebug() << "Failed to start updater.";
                        return;
                    }
                
                    QThread::sleep(5);
                
                    QCoreApplication::quit();
                

                The result is as shown below:

                1. Main QT application starts
                2. It runs the detached process (updater.exe)
                3. Updater exe performs an update and starts a new application
                4. Now I have 2 applications running for a brief moment
                5. After 5 seconds both applications close

                I do not understand why does calling QCoreApplication::quit(); from the main QT application also close a new application that has been started in detached mode from updater.exe

                #TEST2

                Lets assume my main application will not call quit() after starting updater.exe

                1. Main QT application starts
                2. Starts updater.exe in detached mode
                3. updater updates the TestTool and launches a new application
                4. Now I have 2 applications running
                5. Trying to manually close old application will cause both applications to close.
                6. Trying to manually close new application will not cause old application to close.

                I think this is where my issue lies now. Why does new application close when I try to close the old application?

                Could that be because:
                Since the new updated application is started from the updater and the updater is started from the main "old" application, since the updater is child of an "old" application, closing an old application means closing the updater which as a result closes new updated application because the new updated application is child of updater

                Is that how it works?

                artwawA 1 Reply Last reply
                0
                • L lukutis222

                  @artwaw

                  #TEST1

                  In my main application, I can close the application by calling theQCoreApplication::quit() immediately after starting detached application

                      // Start the updater process
                      if (!QProcess::startDetached(updaterExecutable, QStringList() << oldExecutable << newExecutable << QString::number(QCoreApplication::applicationPid()))) {
                          qDebug() << "Failed to start updater.";
                          return;
                      }
                  
                      QCoreApplication::quit();
                  

                  but if I do that, it does not even start updater.exe detached process which is what I am confused about. Is that expected behaviour?

                  For testing purposes, I have added 5 seconds sleep before I quite main application after starting updater:

                      // Start the updater process
                      if (!QProcess::startDetached(updaterExecutable, QStringList() << oldExecutable << newExecutable << QString::number(QCoreApplication::applicationPid()))) {
                          qDebug() << "Failed to start updater.";
                          return;
                      }
                  
                      QThread::sleep(5);
                  
                      QCoreApplication::quit();
                  

                  The result is as shown below:

                  1. Main QT application starts
                  2. It runs the detached process (updater.exe)
                  3. Updater exe performs an update and starts a new application
                  4. Now I have 2 applications running for a brief moment
                  5. After 5 seconds both applications close

                  I do not understand why does calling QCoreApplication::quit(); from the main QT application also close a new application that has been started in detached mode from updater.exe

                  #TEST2

                  Lets assume my main application will not call quit() after starting updater.exe

                  1. Main QT application starts
                  2. Starts updater.exe in detached mode
                  3. updater updates the TestTool and launches a new application
                  4. Now I have 2 applications running
                  5. Trying to manually close old application will cause both applications to close.
                  6. Trying to manually close new application will not cause old application to close.

                  I think this is where my issue lies now. Why does new application close when I try to close the old application?

                  Could that be because:
                  Since the new updated application is started from the updater and the updater is started from the main "old" application, since the updater is child of an "old" application, closing an old application means closing the updater which as a result closes new updated application because the new updated application is child of updater

                  Is that how it works?

                  artwawA Offline
                  artwawA Offline
                  artwaw
                  wrote on last edited by
                  #12

                  @lukutis222 This is indeed a bit peculiar and not an issue I ran into in the past. So perhaps more experienced colleagues here will have an idea? I'd add a 5 seconds delay at the start of the updater, just to make sure original program shuts down (or a flag in the file somewhere that it is closed) so the updater can wait with starting the program again - but that should not be needed in the first place...

                  For more information please re-read.

                  Kind Regards,
                  Artur

                  1 Reply Last reply
                  0
                  • hskoglundH Online
                    hskoglundH Online
                    hskoglund
                    wrote on last edited by
                    #13

                    @lukutis222 you're sure you're using QProcess::startDetached() at all times? Then any spawned child(s) shouldn't die just because the parent dies. (Only exception is when you run the app from the debugger.)

                    For a quick test, create a new empty vanilla Widget app, and in the MainWindow::MainWindow constructor add:

                    QProcess::startDetached("c:/windows/notepad.exe");
                    

                    (don't forget to #include "qprocess.h" at the top)

                    If you build it (in Release mode) and start it, then Notepad should appear. Then if you kill the app from say Task Manager, Notepad should still be alive and well.

                    1 Reply Last reply
                    1
                    • L Offline
                      L Offline
                      lukutis222
                      wrote on last edited by
                      #14

                      Yep you were totally right. I was testing this while running my application in debug mode. If I just simply run the application then it will work without any issues!

                      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