Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QProcess::startDetached use under Windows won't continue when the app exits



  • In many posts here, I see people talking about using startDetached method as a way to have a process continue when the launching app exits. Under Windows, I don't that this is true. I have an installer that I'm starting from my app that will need to overwrite my QT app. When I call the installer program it comes up and then closes right after my app closes. I've tried newing the QProcess and not deleting it as someone suggested elsewhere, but that didn't work either.

    I only need to know that it starts correctly, then I close. Here's my code:

    QProcess * pProcess = new QProcess();

        result = pProcess->startDetached(filename);  //purposely not calling delete
    if (result)
    	QCoreApplication::quit();
    else
        {
            noUpdate = false;  // error starting the installer.
            QMessageBox::warning(this,tr("Oh NO!!"),tr("Couldn't start the installer!!!"),QMessageBox::Ok);
            //delete pProcess;
        }
    

    Can anyone tell me what I need to do to get this to work under Windows?



  • @sdl1

    Hi and welcome to devnet forum

    Try this and ensure that the filename is valid and that you can actually start and keep it running while you are not exiting your application.

    #include <QCoreApplication>
    
    #include <QProcess>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
    //    QProcess::startDetached( "e:/Source/Utilities/testing/Dll.exe" );
    
        QProcess *process = new QProcess;
    
        process->startDetached( "e:/Source/Utilities/testing/Dll.exe" );
    
        return 0;
    }
    

    I have just tested it on windows 10 64 bit with Qt 5.4.2 and Qt 5.9.2 in debug and release. I do not have newer Qt versions installed. The versions tested are pre-compiled for MinGW.

    Both the static and non-static versions behave as expected and documented here



  • I am running Win10 64 bit as well. I don't have any trouble running the app, it just won't stay running. If I don't exit my app (stopped with the debugger), the installer will continue. It's only when I exit that the installer exits. My QT version is 5.11.1 (but it says in parenthesis MSVC2015, 32 bit) I know that my app is being compiled with MinGW. The "filename" shown above is a QString filled in by another function.
    I don't have a Dll.exe program but I used my original program with it and again the installer quits as soon as the main app is gone.



  • Yes, a commandline Window works like I want it to. A Qt Widgets Application works as well, which is what I'm running for my main application.

    So, what would cause this to not work?



  • Apparently, this is because I was running it in the debugger. As soon as I just ran my program instead of debugging it, it just worked! I find that odd!


Log in to reply