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

2

3

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?

Aldredge answered 21/4, 2011 at 12:7 Comment(0)
T
4

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.

Trant answered 21/4, 2011 at 12:10 Comment(0)
M
0

Using NotifyServiceStatusChange requires admin rights.

The service controller does have a ServiceController.WaitForStatus which could be set to listen for changes and create events. It's ugly but it doesn't require admin, so it may be useful in some scenarios.

Mutable answered 28/8, 2023 at 18:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.