如何处理“结束任务”从Windows任务管理器上的后台进程? [英] How to handle "End Task" from Windows Task Manager on a background process?

查看:923
本文介绍了如何处理“结束任务”从Windows任务管理器上的后台进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的测试程序(TestProgram.exe)来学习如何处理CTRL_CLOSE_EVENT,这里是我的观察和我的问题:

I wrote a simple test program (TestProgram.exe) to learn how to handle the CTRL_CLOSE_EVENT and here are my observations and my question:

1)单击TestProgram.exe启动它,如果我现在去任务管理器,TestProgram.exe列在应用程序下。当我在TestProgram.exe上执行结束任务时,我的CTRL_CLOSE_EVENT的处理程序正在调用。

1) When I double click TestProgram.exe to launch it, and if I now go to Task Manager, TestProgram.exe is listed under "Apps". When I do "End Task" on TestProgram.exe, my handler for CTRL_CLOSE_EVENT is getting called.

但是

2)当我打开一个命令提示符并启动TestProgram.exe时,它被列在任务管理器下的后台进程下,并对其执行结束任务不会导致CTRL_CLOSE_EVENT。

2) When I open a command prompt and launch TestProgram.exe, it is listed under "Background Processes" under Task Manager and doing an "End Task" on the same doesn't result in a CTRL_CLOSE_EVENT.

我的实际应用程序使用如上面情况2)中所述。我想在用户在我的应用程序(在任务管理器中的后台进程下列出)中执行结束任务时进行一些清理。

My real application is used as described in case 2) above. I want to do some cleanup when users do an End Task on my app (which is listed under Background processes in Task Manager).

谢谢,
Krishna

Thanks, Krishna

推荐答案

一般来说,当进程被列为应用程序时,意味着任务管理器检测到进程有一个GUI,并且GUI上的结束任务将首先尝试通过标准 WM_CLOSE 和/或 WM_QUIT 消息,然后通过 TerminateProcess()求助于强制终止GUI的进程。另一方面,对后台进程执行结束任务将立即执行强制终止。

In general, when a process is listed as an "Application", it means Task Manger has detected the process has a GUI, and an "End Task" on a GUI will first attempt to graceful close the GUI via standard WM_CLOSE and/or WM_QUIT messages before then resorting to a brute-force termination of the GUI's process via TerminateProcess(). On the other hand, doing an "End Task" on a "Background Process" will perform a brute-force termination immediately.

因此,在您的情况下,双击.exe文件导致一个新的专用控制台进程,它只运行你自己的应用程序,所以控制台的GUI被标记为应用程序,但是当您首先打开控制台窗口并通过命令行执行您的.exe ,您的应用程序在现有控制台中运行,并且共享控制台的原始GUI,因此您的应用程序没有自己的GUI,因此会被标记为后台进程。

So in your situation, double-clicking on the .exe file results in a new dedicated console process that is running just your app by itself, so the console's GUI gets flagged as an "Application", but when you open a console window first and execute your .exe via the command-line, your app is running within the existing console and is sharing the console's original GUI, so your app does not have its own GUI and thus gets flagged as a "Background Process" instead.

这篇关于如何处理“结束任务”从Windows任务管理器上的后台进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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