QThread::terminate vs 击杀

QThread::terminate vs kill

我在 QThread(C++ 中)中有一个 BASH 脚本 运行(QProcess 阻塞)。这个 BASH 脚本可以打包很多文件,并且可以 运行 1/2 小时。

如果用户想要关闭我的程序,我需要终止我的 BASH 脚本。但是怎么办? QThread::Quit 将等待 BASH 程序在处理信号之前终止,QThread::Terminate 文档说它可能会立即终止线程。

我想要 'kill -9 myscript' 的等价物。有合适的 Qt 方法来做到这一点吗?

  1. 不要使用额外的线程。从来没有必要。
  2. 切勿使用任何 waitForXxx 方法。
  3. 使用QProcess::kill终止进程。
  4. 使用 QProcess 的信号在进程改变状态时得到通知,例如完成了。

I want the equivalent of 'kill -9 myscript'. Is there a proper Qt way to do this ?

来自 Qt 的文档 http://doc.qt.io/qt-5/qprocess.html#kill :

void QProcess::kill()

Kills the current process, causing it to exit immediately.

On Windows, kill() uses TerminateProcess, and on Unix and macOS, the SIGKILL signal is sent to the process.