我怎样才能在状态Windows服务的变化做出反应?反应、状态、Windows

2023-09-04 02:52:14 作者:幼儿园园长助理i

我不知道是否有任何可能的方式来获取或状态改变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及更高版本。如果您需要针对早期版本中,你可以找到答案的一个解决方案,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.