在C#中从任务管理器检测最终进程 [英] Detect End Process From Task Manager In C#

查看:328
本文介绍了在C#中从任务管理器检测最终进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用C#编写的应用程序,并且想监视它的当前状态,即它的运行状态还是关闭状态.到目前为止,如果应用程序在运行时错误期间退出,或者其他任何应用程序退出代码,我都可以跟踪状态.

I have an application in C#, and would like to monitor its current status, i.e. whether its running or closed. So far i am able to track the status if the application exits during a run time error, or any other in application exit codes.

但是当任务管理器强制关闭应用程序状态时,我无法登录.

But i am unable to log the application status when it is force closed by task manager.

是否有一种方法可以监视结束流程"事件?还是我必须设计一个单独的服务来监视我的应用程序(我发现这会浪费资源)?

Is there a way to monitor End Process events? or do i have to design a separate service to keep a watch on my application (which i find would be a waste of resources)?

简史: 该应用程序是WCF订户应用程序,旨在作为后台进程运行并从服务器接收数据.我不想使用Windows服务,因为我正在设计一个GUI,该GUI允许客户端将请求发送给服务器以获取特定数据.

Brief History: The application is a WCF subscriber Application that is meant to run as a background process and receive data from the server. I don't want to use a windows service as i am designing a GUI which allows the clients to send requests to the server for specif data.

此应用程序链接到另一个跟踪订户的应用程序,无论该订户是联机还是脱机.

This application is linked to another application which keeps track of the subscriber whether, the subscriber is online or offline.

因此,我们一直在寻找一种跟踪应用程序突然关闭并相应地通知用户的方法.

Thus was looking for a way to track abrupt application close and notify the users accordingly.

推荐答案

我肯定以前在SO上看到过这样的问题,所以这可能是重复的,但是我找不到它,所以...

I'm sure I have seen a question like this on SO before, so this is probably a duplicate, but I cannot find it, so...

这里是一个简短的总结:

Here is a short wrapup:

  • 如果在进程"选项卡中选择一个进程,然后单击结束进程",则使用TerminateProcess()将杀死该进程-您甚至无法从进程中拦截该进程,更不用说阻止它了.

  • If you select a process in "Processes" tab and click "End Process" the process is being killed using TerminateProcess() - there is no way you can even intercept that from your process, let alone, prevent it.

如果从应用程序"标签中选择应用程序"并单击结束任务",则发生的情况取决于应用程序的类型:

If you select an "application" from the "Applications" tag and click "End Task", what happens depends on the type of application:

  • 正在向控制台应用程序发送CTRL_CLOSE_EVENT事件.
  • Windows/GUI应用程序正在发送WM_CLOSE消息.
  • Console Applications are being send a CTRL_CLOSE_EVENT event.
  • Windows/GUI Applications are being send a WM_CLOSE message.

您可以同时响应(或处理日志等).

You could respond (or handle with logging, etc.) both.

更新:只是喜欢 更新2 :找到一个/重复项并投票结束.我暂时在这里留下这个答案.

Update 2: found a/the duplicate and voting to close. I leave this answer here for now.

这篇关于在C#中从任务管理器检测最终进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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