4

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.

László Papp
  • 51,870
  • 39
  • 111
  • 135
Konstantin Ivanov
  • 245
  • 1
  • 3
  • 6

1 Answers1

9

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
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • This is a correct solution and helped me suppress the warning. I would however suggest longer timeout than just 1 ms in `p.waitForFinished(1);`. The process termination can take just a bit longer in some cases, I think. I am using -1 to have no timeout (believing that the OS will really kill the process in a reasonable time). – HiFile.app - best file manager May 16 '19 at 09:34