处理Windows应用程序的结束进程

问题描述:

是否可以在同一Windows应用程序本身捕获Windows应用程序的任务管理器结束进程?我正在使用一个C#2.0的胜利应用程序,我想做一些数据库处理(在DB中将标志从'Y'更改为'N')。

Is it possible to capture the task manager end process of a windows application within the same windows application itself? I am using a C# 2.0 win app and I would like to do some database processing (change a flag from 'Y' to 'N' in the DB) when an end process happens.

不,挂钩操作系统决定结束进程是不可能的。注意,这不是由任务管理器完成的,结束一个进程是内核的责任。

No, it is not possible to hook the operating system's decision to end a process. Note, this is not done by task manger, ending a process is the responsibility of the kernel.

您需要在此处执行两件事:

You will need to do two things here:


  1. 将事件处理程序连接到告诉应用程序退出的普通用户界面消息。使用这些事件来保存数据,免费资源,否则退出干净。

  2. 如果可能,处理异常以捕获错误并清除并保存数据。

这里有三个链接到Raymond的博客,解释为什么你不能做你所要求的。

Here are a three links to Raymond's blog explaining why you cannot do what you are asking.

  • Why can't you trap TerminateProcess?
  • Why do some process stay in Task Manager after they've been killed?
  • The arms race between programs and users

另外,我发现了类似的*问题这里

Also, I addressed a similar * question here.