3

Hi I am using the ServiceController class to start and stop a windows service, I can also query the Status property to detect whether the service is running or not. I'd like to monitor and display the status of this service in my application. Unfortunately this seems to be only possible by polling the Status property. I'd rather use some notification mechanism.

Is there any event in .NET which can notify me that the service status has changed?

Rob van Groenewoud
  • 1,824
  • 1
  • 13
  • 17

3 Answers3

1

If you are targeting Windows Vista or higher you can call NotifyServiceStatusChange

WMI will poll the services for you. Check How can I monitor status changes of windows services under windows xp?

Community
  • 1
  • 1
Sheng Jiang 蒋晟
  • 15,125
  • 2
  • 28
  • 46
0

Not that I know of, but you could look at using WMI

http://msdn.microsoft.com/en-us/library/bb404655.aspx

Doobi
  • 4,844
  • 1
  • 22
  • 17
0

Unless they've added something in .NET 4.0, you'll have to poll. This is the approach that I am currently using my project. I created a class that allows interested parties to specify which service to monitor. A background thread is then started that monitors the service status once every second. If a change in status is detected, an event is fired to notify the interested parties of the old and new statuses.

Matt Davis
  • 45,297
  • 16
  • 93
  • 124