当某人在C#中杀死进程时,Windows服务onstop()事件未被触发 [英] Windows service onstop() event not fired when someone kills the process in C#

查看:578
本文介绍了当某人在C#中杀死进程时,Windows服务onstop()事件未被触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows服务。当有人停止我的服务时,我正试图发送邮件。

但当有人杀死进程时,OnStop事件没有被解雇。

当有人通过任务管理器手动停止服务时,它会起作用。当有人杀死这个过程时如何收到邮件。



我尝试过:



protected override void OnStop()

{

SendMail();

}

I have a windows service. I am trying to send mail when someone stops my service.
But when someone kills the process then OnStop event not fired.
It works when someone manually stops service through task manager.How to get mail when someone kills the process.

What I have tried:

protected override void OnStop()
{
SendMail();
}

推荐答案

杀死任务不是正常行为:它与停止它不同。杀死一个任务是一个激进的操作系统活动,它立即终止线程,并将进程内存返回给Windows - 它不会尝试对进程做任何事情以确保有序关闭,刷新流或其他任何东西 - 它是操作系统取出一个雇员的合同,而不是给他四个星期的通知退出!



当用户杀死你的过程时,就是这样:你没有得到任何警告它会发生。发送电子邮件的唯一方法是使用单独的服务监控原始服务的运行状况,并在异常终止时发送电子邮件。
Killing a task is not a "normal" behaviour: it is not the same as stopping it. Killing a task is a radical OS activity which terminates threads immediately, and returns process memory to Windows - it does not attempt to do anything to the process to ensure an orderly shutdown, flush streams, or anything else - it is the operating system taking out a contract on an employee instead of giving him four weeks notice to quit!

When the user kills your process, that's it: you don't get any warning it will occur. The only way you can get your email sent is to have a separate service which monitors the original services' health and sends the email when it is terminated abnormally.


唯一的选择是防止杀死该服务。看起来并不容易,请参见此处的讨论:在Windows中创建无法确定的进程 - 信息安全堆栈交换 [ ^ ]



一个弱小的解决方案是有一个守卫服务,它可以观察到你的主要服务的存在。



我希望这会有所帮助。
The only option you have is to prevent from killing the Service. It seems not to be easy, see discussion here: Create a unterminable process in Windows - Information Security Stack Exchange[^]

A weak solution is to have a guard Service which observes the existence of your main Service.

I hope this helps.


这篇关于当某人在C#中杀死进程时,Windows服务onstop()事件未被触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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