我怎样才能在状态Windows服务的变化做出反应? [英] How can I respond to a change in status for a Windows Service?

查看:182
本文介绍了我怎样才能在状态Windows服务的变化做出反应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有任何可能的方式来获取或状态改变Windows服务的创建活动。

I wonder if there is any possible way to get or create an event for a status changed of a Windows Service.

我知道的ServiceController 类没有的情况下,但它的地位。有没有什么办法,我可以听一个事件?

I know that the ServiceController class does not have the event, but it has the status. Is there any way that I can listen to an event?

推荐答案

这正是的 NotifyServiceStatusChange 功能仅供。该文档说的:

This is exactly what the NotifyServiceStatusChange function is intended for. The docs say that it:

允许应用程序创建时指定的服务或删除或当其状态更改为接收通知。

Enables an application to receive notification when the specified service is created or deleted or when its status changes.

我不知道是否有等效事件裹在管理code,但是这个人是很容易获得,在使用P / Invoke。

I'm not sure if there's an equivalent event wrapped in managed code, but this one is easy enough to get at using P/Invoke.

不过,请注意,此功能仅在Windows Vista及更高版本。如果您需要针对早期版本中,你可以找到答案的一个解决方案,<一个href="http://stackoverflow.com/questions/1189815/how-can-i-monitor-status-changes-of-windows-services-under-windows-xp">this问题。

However, note that this function is only available in Windows Vista and later. If you need to target earlier versions, you can find a solution in one of the answers to this question.

这篇关于我怎样才能在状态Windows服务的变化做出反应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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