Best way to determine if a .NET Windows service is hanging?

What is the best way to actively monitor if the .NET Windows service is still running (for example, not hanging)? The service must periodically respond to events triggered by a timer.

thanks

John

+4
source share
4 answers

Non-freezing is always a difficult condition for final detection 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 mapping to shared memory with a name and simply periodically record information from your service. Another process can monitor shared memory and see if its state stops changing.

The advantage of this approach is that you can expand the shared memory block to include other diagnostic tools that can help diagnose any problems.

+2
source

One way is to create your own performance counter in .NET:

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

And then the counter will be tied to "Seconds Active" with a timer.

+2
source

The obvious solution seems to be checking if everything is responding. So add a low frequency watchdog. I don’t know where you want to control liveliness, but in most places you can read from a named pipe. Then translate the watchdog event into an entry in the named pipe. If you do not observe a read, you know that your service does not respond to at least one timer.

0
source

You could create a named event or mutex, and then switch it as the code executes. Then the external program can see this pulsation as an indication that the program is running.

I also used an internal watchdog system running on a different thread. This thread considers the main thread for activity, such as a log output or a switch event. If the activity is not visible, the service is considered to be hung, and I end the service. In this case, you can configure windows to automatically restart a stopped service, which can fix the problem (if this is not an internal logic error).

Also, the services I work with have text logs that are written to the log. In addition, for services that are going to "get some sleep," I am recording the time for the next wake up. I use MTAIL to view the log for output.

0
source

All Articles