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

假装没事ソ 提交于 2019-12-22 10:59:56

问题


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

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?


回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/5743836/how-can-i-respond-to-a-change-in-status-for-a-windows-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!