Click here to Skip to main content
15,892,059 members
Articles / Programming Languages / C#
Tip/Trick

Simple C# Watchdog

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
20 Dec 2015CPOL1 min read 59.2K   5.9K   14   4
This tip will describe the use of a simple watchdog process to monitor and revive another application.

Introduction

The watchdog process monitors and revives another application, should there be a failure. This can be used as a simple recovery technique to ensure application availability in critical environments. The WatchdogLib contains all the necessary utilities to implement and integrate a simple watchdog to your .NET applications.

Background

This post is about implementing a simple Watchdog application to revive another application. I was responsible for a mission-critical application that should run on a Windows POS device and I had to ensure the availability of that application throughout the operation time. For this, a Watchdog application was introduced.

A simple Watchdog application monitors another application process periodically and revives the monitored application if its process gets terminated due to a system failure. The monitored application should also monitor the Watchdog application and awaken it if required. Additionally, the Watchdog can be used to detect application hangs or freezes. This can be accomplished by using a heartbeat mechanism between the monitored and Watchdog applications.

Using the Code

The WatchdogLib has two main classes:

  • ApplicationWatcher (the watchdog)
  • WatchdogWatcher (the watchdog's watchdog)

Using the ApplicationWatcher

C#
ApplicationWatcher applicationWatcher = new ApplicationWatcher("MonitoredApplication", "WatchDog", 5000);

The ApplicationWatcher class is the actual watchdog. The ApplicationWatcher constructor takes in the monitored application name, watchdog application name and the preferred monitoring interval and initiates the watchdog process.

Using the WatchdogWatcher

C#
WatchdogWatcher watchdogWatcher = 
	new WatchdogWatcher("WatchDog", "WatchDog.exe", watchDogMonitorInterval);

The WatchdogWatcher class is integrated with the monitored application and it is responsible for observing the watchdog. The WatchdogWatcher constructor takes in the watchdog application name, watchdog executable path name and the preferred monitoring interval and initiates the watchdog watcher.

History

  • Initial version(v 1.0.0.0) of the WatchdogLib added

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Sri Lanka Sri Lanka
I am a versatile software engineer. My expertise range from mobile, embedded to server and web solutions. I have experience in a vast array of technologies, platforms and architectures. I am highly devoted on developing optimal solutions and maintaining a high level of attention to detail and testing. I am willing to accept any challenge, innovate and excel beyond expectations.

Comments and Discussions

 
QuestionIf i understand Well Pin
Yves11-May-16 10:41
Yves11-May-16 10:41 
SuggestionRe: If i understand Well Pin
roshfsk17-May-16 20:03
professionalroshfsk17-May-16 20:03 
GeneralRe: If i understand Well Pin
Member 824837924-May-17 23:36
Member 824837924-May-17 23:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.