简体   繁体   English

如何响应Windows服务的状态更改?

[英]How can I respond to a change in status for a Windows Service?

I wonder if there is any possible way to get or create an event for a status changed of a Windows Service. 我想知道是否有任何可能的方法来获取或创建Windows服务状态更改的事件。

I know that the ServiceController class does not have the event, but it has the status. 我知道ServiceController类没有事件,但它具有状态。 Is there any way that I can listen to an event? 有什么方法可以听一个事件吗?

This is exactly what the NotifyServiceStatusChange function is intended for. 这正是NotifyServiceStatusChange函数的用途。 The docs say that it: 文档说它:

Enables an application to receive notification when the specified service is created or deleted or when its status changes. 允许应用程序在创建或删除指定服务或其状态更改时接收通知。

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. 我不确定托管代码中是否包含等效事件,但是这个事件很容易使用P / Invoke。

However, note that this function is only available in Windows Vista and later. 但请注意,此功能仅适用于Windows Vista及更高版本。 If you need to target earlier versions, you can find a solution in one of the answers to this question . 如果您需要定位早期版本,可以在此问题的答案之一中找到解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM