tags:

views:

410

answers:

4

What is the best way to actively monitor if a .NET Windows service is still running (e.g not hung)? The service needs to periodically respond to events raised by a timer.

Thanks

Jon

+1  A: 

"Not hung" is always a hard condition to definitively detect, and will depend on the details of your service.

In general any form of interprocess communication can be used. One simple approach is to create a named shared memory mapping and simply write information to it periodically from your service. Another process can monitor the shared memory and see if its state stops changing.

The advantage of this approach is that you can extend the shared memory block to include other diagnostics which may help diagnose any issues.

Rob Walker
+2  A: 

One way is you can create your own Performance Counter in .NET:

http://msdn.microsoft.com/en-us/library/5e3s61wf(VS.71).aspx

And then have the counter be tied to a "Seconds active" with a timer.

Chris Roland
A: 

The obvious solution seems to be, check if it's still responding. So add a low-frequency watchdog timer. I don't know where you want to monitor liveliness, but in most places you should be able to read from a named pipe. Then translate the watchdog timer event into a write to the named pipe. If you don't observe reads, you know your service is failing to respond to at least one timer.

MSalters
A: 

You could created a named event or mutex and then toggle it as your code is executing. Then an external program can see this pulsing as an indication that the program is running.

I have also used an internal watchdog system running in another thread. That thread looks at the main thread for activity like log output or a toggling event. If the activity is not seen then the service is considered hung and I shutdown the service. In this case you can configure windows to auto-restart a stopped service and that might clear the problem (as long as it's not an internal logic bug).

Also services I work with have text logs that are written to a log. In addition for services that are about to "sleep for a bit", I log the time for the next wake up. I use MTAIL to watch a log for output.

John Dyer