Updating main QT application using updater application
-
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.
-
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.
@artwaw This sound like exactly my approach. Why do you think mine is different?
-
@artwaw This sound like exactly my approach. Why do you think mine is different?
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.
-
@artwaw This sound like exactly my approach. Why do you think mine is different?
@lukutis222
Absolutely as @artwaw has written. Additionally what is thiskill
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! -
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.
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
-
@lukutis222
Absolutely as @artwaw has written. Additionally what is thiskill
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!@JonB
I am trying to kill old application. The process is as following:- Run main QT application which starts updater.exe in detached mode but it does not end current application.
- Updater.exe renames the old application with TestTool_renamed.exe and places new exe as TestTool.exe
- Updater.exe runs the TestTool.exe as detached process
- 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? -
@JonB
I am trying to kill old application. The process is as following:- Run main QT application which starts updater.exe in detached mode but it does not end current application.
- Updater.exe renames the old application with TestTool_renamed.exe and places new exe as TestTool.exe
- Updater.exe runs the TestTool.exe as detached process
- 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?Always via the old application itself.
The safest way to do so is to callclose()
method of the main window (which also happens to be a slot should you need it). I don't know whyQCoreApplication
static call doesn't work but it's not the method one would normally use anyway. -
Always via the old application itself.
The safest way to do so is to callclose()
method of the main window (which also happens to be a slot should you need it). I don't know whyQCoreApplication
static call doesn't work but it's not the method one would normally use anyway.#TEST1
In my main application, I can close the application by calling the
QCoreApplication::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:
- Main QT application starts
- It runs the detached process (updater.exe)
- Updater exe performs an update and starts a new application
- Now I have 2 applications running for a brief moment
- 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 startingupdater.exe
- Main QT application starts
- Starts
updater.exe
in detached mode - updater updates the TestTool and launches a new application
- Now I have 2 applications running
- Trying to manually close old application will cause both applications to close.
- 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 theupdater
and theupdater
is started from the main "old" application, since theupdater
is child of an "old" application, closing an old application means closing theupdater
which as a result closes new updated application because the new updated application is child ofupdater
Is that how it works?
-
#TEST1
In my main application, I can close the application by calling the
QCoreApplication::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:
- Main QT application starts
- It runs the detached process (updater.exe)
- Updater exe performs an update and starts a new application
- Now I have 2 applications running for a brief moment
- 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 startingupdater.exe
- Main QT application starts
- Starts
updater.exe
in detached mode - updater updates the TestTool and launches a new application
- Now I have 2 applications running
- Trying to manually close old application will cause both applications to close.
- 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 theupdater
and theupdater
is started from the main "old" application, since theupdater
is child of an "old" application, closing an old application means closing theupdater
which as a result closes new updated application because the new updated application is child ofupdater
Is that how it works?
@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...
-
@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.
-
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!