QProcess::kill() 和 QProcess::terminate() 有什么区别?

What's the difference between QProcess::kill() and QProcess::terminate()?

我阅读了一些文档,但对我来说还不够清楚。我知道 "end" 进程和 kill() 都是为了强制它结束,但是 terminate() 应该做什么呢?

不知道你写的有什么不清楚的:

void QProcess::kill()

Kills the current process, causing it to exit immediately.

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

http://doc.qt.io/qt-5/qprocess.html#kill


void QProcess::​terminate()

Attempts to terminate the process.

The process may not exit as a result of calling this function (it is given the chance to prompt the user for any unsaved files, etc).

On Windows, terminate() posts a WM_CLOSE message to all toplevel windows of the process and then to the main thread of the process itself. On Unix and OS X the SIGTERM signal is sent.

Console applications on Windows that do not run an event loop, or whose event loop does not handle the WM_CLOSE message, can only be terminated by calling kill().

http://doc.qt.io/qt-5/qprocess.html#terminate

所以,基本上 ​terminate() 没那么残酷,但不保证进程会被终止。

在 Unix 上 terminate() 使用 SIGTERM 信号,而 kill() 向进程发送 SIGKILL。它们之间的区别是 SIGTERM 可以被一个进程捕获,这允许它执行清理等。SIGTERM 可以被忽略。 SIGKILL 会直接杀死进程,进程不能忽略它。

在 Windows WM_CLOSE message is posted, when you call terminate(), so application can also gracefully handle it. kill() calls TerminateProcess() 上,这或多或少 Windows 相当于 SIGKILL。

我认为 terminate() SIGTERM 和 WM_CLOSE 可以由 Qt 处理并转换为正常的 Qt 事件,但您必须自己尝试。您当然可以通过系统特定的功能来处理它们。


“是什么原因导致 terminate() 不退出进程。”

是你,因为你可以抓住 terminate() signals/messages 并为所欲为,或者如果他真的想退出应用程序,如果提示他,它可以是你的应用程序的用户。 Yet another resource on WM_CLOSE.