Question

Simplest code:

void test
{
    QProcess p;
    p.start("sleep 10");
    p.waitForBytesWritten();
    p.waitForFinished(1);
}

Of course, the process can't be finished before the end of the function, so it displays a warning message:

 QProcess: Destroyed while process ("sleep") is still running.

I want this message not to be shown - I should destroy the process by myself before end of function, but I can't find how to do this correctly: p.~QProcess(), p.terminate(), p.kill() can't help me.

NOTE: I don't want wait for process execution, just kill it when its running, by myself.

Was it helpful?

Solution

You can kill or terminate the process explicitly depending on your desire. That is, however, not enough on its own because you actually need to wait for the process to terminate. "kill" means it will send the SIGKILL signal on Unix to the process and that also takes a bit of time to actually finish.

Therefore, you would be writing something like this:

main.cpp

#include <QProcess>

int main()
{
    QProcess p;
    p.start("sleep 10");
    p.waitForBytesWritten();
    if (!p.waitForFinished(1)) {
        p.kill();
        p.waitForFinished(1);
    }
    return 0;
}

main.pro

TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top