关闭应用程序和从任务管理器结束进程有什么区别? [英] What is the difference between closing an application and ending the process from Task Manager?

查看:88
本文介绍了关闭应用程序和从任务管理器结束进程有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用关闭按钮终止应用程序和从任务管理器结束进程有什么区别?

What is the difference between killing an application using the close button and ending the process from the Task Manager?

我知道点击关闭按钮会在消息队列中发布 WM_CLOSE 消息,但我不知道当我们从任务管理器(或任何类似的Killbox 或 Process Explorer 等应用程序).

I am aware of the fact that hitting the close button posts a WM_CLOSE message in the message queue, but I don't know what happens when we kill a process from Task Manager (or any similar application like Killbox or Process Explorer).

推荐答案

当您单击应用程序窗口标题栏中的X"按钮时,会向该窗口发送一个 WM_CLOSE 消息.这是一种正常"关闭——应用程序处理消息,处理任何必要的清理任务,甚至可以根据需要拒绝关闭(通过响应消息返回零).WM_CLOSE 只是窗口或应用程序终止的请求;在应用程序本身调用 DestroyWindow 函数之前,窗口不会被销毁.

When you click the "X" button in the title bar of an application's window, that sends the window a WM_CLOSE message. This is a "graceful" shutdown—the application processes the message, handles any necessary cleanup tasks, and can even refuse to shut down if it so desires (by returning zero in response to the message). WM_CLOSE is simply a request that the window or application terminate; the window is not destroyed until the application itself calls the DestroyWindow function.

当您在任务管理器中按下结束任务"按钮时,Windows 将首先尝试向应用程序(如果它是 GUI 应用程序)发送 WM_CLOSE 消息.换句话说,它首先很好地询问并让应用程序有机会干净地终止自己.*

When you press the "End Task" button in Task Manager, Windows will first try to send the application (if it is a GUI application) a WM_CLOSE message. In other words, it first asks nicely and gives the app a chance to terminate itself cleanly.*

如果您未能关闭对最初的 WM_CLOSE 消息的响应,任务管理器将通过调用 TerminateProcess 函数.这个函数有点不同,因为它强行终止应用程序的进程及其所有线程,而无需请求应用程序的许可.这是一种非常苛刻的关闭方法,应该作为最后的手段使用 - 例如当应用程序挂起并且不再响应消息时.

If you fail to close in response to that initial WM_CLOSE message, the Task Manager will follow up by calling the TerminateProcess function. This function is a little bit different because it forcefully terminates the application's process and all of its threads without asking for permission from the app. This is a very harsh method of closing something, and should be used as a last resort—such as when an application is hung and is no longer responding to messages.

TerminateProcess 是一个非常低级的函数,它本质上从内存中提取进程的用户模式部分,迫使它无条件终止.调用 TerminateProcess 绕过关闭通知和 DLL_PROCESS_DETACH 等细节.您的应用程序没有拒绝关闭的能力,也无法捕获/捕获/钩住对 TerminateProcess 的调用.进程中的所有用户模式代码只是永久停止运行.这是一个非常不干净的关机程序,有点类似于将计算机的电源插头从墙上拔下来.

TerminateProcess is a very low-level function that essentially rips the user-mode part of a process from memory, forcing it to terminate unconditionally. Calling TerminateProcess bypasses such niceties as close notifications and DLL_PROCESS_DETACH. Your application does not have the ability to refuse to close, and there is no way to catch/trap/hook calls to TerminateProcess. All user-mode code in the process simply stops running for good. This is a very unclean shut down procedure, somewhat akin to jerking the computer's power plug out of the wall.

* 请注意,这仅在您使用任务管理器的应用程序"选项卡终止应用程序时才适用.如果您使用Processes"选项卡,则会跳过此步骤并立即调用 TerminateProcess 函数.这种区别反映在相应按钮的标题中.对于应用程序"选项卡,按钮标记为结束任务";对于进程"选项卡,按钮标记为结束进程".

* Note that this only true if you use the "Applications" tab of Task Manager to kill the application. If you use the "Processes" tab, this step is skipped and the TerminateProcess function is called immediately. This distinction is reflected in the caption on the respective buttons. For the "Applications" tab, the button is lableled "End Task"; for the "Processes" tab, the button is labeled "End Process".

这篇关于关闭应用程序和从任务管理器结束进程有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆