简体   繁体   English

QThread ::终止vs杀死

[英]QThread::terminate vs kill

I have a BASH script run (QProcess blocking) in a QThread (in C++). 我在QThread中运行了一个BASH脚本(QProcess阻塞)(在C ++中)。 This BASH script tars lots of files and can run for 1/2 hour. 这个BASH脚本包含大量文件,可以运行半小时。

In case the user wants to shutdown my program I need to kill my BASH script. 如果用户想要关闭我的程序,我需要杀死我的BASH脚本。 But how? 但是怎么样? QThread::Quit will wait for the BASH program to terminate before processing signals, QThread::Terminate documentation says it MAY kill a thread immediately. QThread :: Quit将等待BASH程序在处理信号之前终止,QThread :: Terminate文档说它可能会立即杀死一个线程。

I want the equivalent of 'kill -9 myscript'. 我想要相当于'kill -9 myscript'。 Is there a proper Qt way to do this? 有没有一个合适的Qt方式来做到这一点?

  1. Do not use additional threads. 不要使用其他线程。 It's never necessary. 这绝不是必要的。
  2. Never use any waitForXxx methods. 永远不要使用任何waitForXxx方法。
  3. Use QProcess::kill to kill the process. 使用QProcess::killQProcess::kill进程。
  4. Use QProcess 's signals to get notified when the process changes state, eg is finished. 使用QProcess的信号在过程改变状态时得到通知,例如完成。

I want the equivalent of 'kill -9 myscript'. 我想要相当于'kill -9 myscript'。 Is there a proper Qt way to do this ? 有没有一个合适的Qt方式来做到这一点?

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

void QProcess::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. 在Windows上,kill()使用TerminateProcess,在Unix和macOS上, SIGKILL信号被发送到进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM