找出我的Windows服务正在关闭的原因 [英] Find out why my Windows Service is Closing

查看:153
本文介绍了找出我的Windows服务正在关闭的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我正在编写一个简单的Windows服务,定期跟踪一些注册表项。我希望在人员停止服务时发送警报电子邮件。



当我环顾四周时,我发现这个有趣的报价:

Ok, so I''m writing a simple Windows Service that keeps track of a few Registry entries periodically. I want to send out an alert email when the service is stopped by a human.

I found this interesting quote when I was looking around:

您可以检查关闭条件以查看它是否为任务管理器,如果是,则将取消值设置为true - 意味着取消结束。

You can check the close condition to see if it is Task Manager and if it is, then set the ''cancel'' value to true - meaning cancel the closing.



我不知道这家伙是什么意思。什么是关闭条件?

谁能详细说明如何做这类东西?





谢谢!


I don''t know what this guy means. What''s the "close condition"?
Can anyone elaborate on how to do that kind of stuff?


Thanks!

推荐答案

请求错误!请看我对这个问题的评论。如果您认为网上发布的任何愚蠢行为都会引起轰动,那么对您来说这可能会成为一个巨大的问题。



此外,还有将服务用于投票的整个想法周期性基础上的条件是错误的。所以,我要说的第一件事是:你不需要这样的服务。当没有任何反应时,你会浪费CPU时间检查一些东西;与此同时,您可以轻松地错过此事件,因为有人可以在您的民意调查之间更改您的注册表。你正在做一些几乎没有任何意义的事情。也许如果你解释为什么这样做,你就有机会获得一些合理的建议。







问题不是搜索语言,问题是轮询,即使在更简单的情况下也是如此;我在上面解释了它的一部分。



为了解你的投票(拉动与推动),请参阅:

http://en.wikipedia.org/wiki/Push_technology [ ^ ],

http://en.wikipedia.org/wiki/Pull_technology [ ^ ]。



特别是,这个类提供了推送方法的好处:

http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx [ ^ ]。



您订阅了某类活动在文件系统上,所有修改都被推送到你的处理程序代码。



我不是说这个课程对你来说是最好的解决方案,它''更多的是给你这个想法。我可能会或可能不知道有关注册管理机构问题的最终解决方案;这取决于你想要达到的目标。为什么这样做?



无论如何,你真的需要检测注册表更改,你需要使用推送技术。实现此目的的一种方法是使用WMI订阅事件。请参阅:

http://msdn.microsoft.com/en-us /library/aa393035.aspx [ ^ ]。



[结束编辑]



关于停止人类服务,解决方案是微不足道的。在您的类 System.ServiceProcess.ServiceBase 的实现中,您甚至可以通过覆盖虚拟 OnStop 方法来处理Stop :

http://msdn.microsoft.com/en -us / library / system.serviceprocess.servicebase.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onstop.aspx [ ^ ]。



就这么简单。问题解决了。



-SA
The request is so incorrect! Please see my comment to the question. If you think that any foolishness posted on the Web makes sens, it can become a huge problem for you.

Also, the whole idea of having the service to poll any condition on periodic basic is wrong. So, first thing I would say is: you don''t need such "service". You waste CPU time on checking up something when nothing happens; at the same time, you can easily miss the event, because someone can change your registry between your polls. You are doing something which hardly can make any sense. Perhaps if you explain why doing so, you can get a chance for some reasonable advice.



The problem is not a search language, the problem is polling, which is always bad, even in simpler cases; I explained a part of it above.

For your understanding of the polling (pull vs push), please see:
http://en.wikipedia.org/wiki/Push_technology[^],
http://en.wikipedia.org/wiki/Pull_technology[^].

In particular, this class provides a benefit of the push approach:
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx[^].

You subscribe to a certain class of events on the file system, and all modifications are pushed to your handler code.

I don''t say this class could be a best solution for you, it''s more to give you the idea. I may or may not know the ultimate solution about the Registry issue; it depends on what you want to achieve, exactly. Why doing so?

Anyway, is you really need to detect registry changes, you need to use the push technology. One way to achieve this is subscribing to the events using WMI. Please see:
http://msdn.microsoft.com/en-us/library/aa393035.aspx[^].

[END EDIT]

As to the stopping a service by human, the solution is trivial. In your implementation of the class System.ServiceProcess.ServiceBase, you can handle the Stop even by overriding the virtual OnStop method:
http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.aspx[^],
http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onstop.aspx[^].

As simple as that. Problem solved.

—SA


这篇关于找出我的Windows服务正在关闭的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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