Click here to Skip to main content
15,881,424 members
Articles / Programming Languages / C#

Watch Dog

Rate me:
Please Sign up or sign in to vote.
3.76/5 (22 votes)
5 Sep 2007CPOL4 min read 144K   3.3K   84  
Execute applications with a Windows Service and ensure its existance.
using System;
using System.Threading;

namespace WatchDogPile
{

	public interface ITrainingDog
	{
		object SuspectedSmell();
	}

	#region WatchDogEventArgs: Event will fire in intervals
	public class WatchDogEventArgs: EventArgs 
	{
		public object msg;
		public WatchDogEventArgs(object o_msg)
		{
			msg = o_msg;
		}
	}    
	#endregion

	/// <summary>
	/// WatchDog to check the engines(Processes) running okay
	/// </summary>
	public class WatchDog: IDisposable
	{
		/// <summary>
		///  event will be fired when SuspectedSmell() of ITrainingDog returns true
		/// </summary>
		#region Watch dog barks
		public delegate void BarkEventHandler(object source, WatchDogEventArgs rea);
		public event BarkEventHandler BarkEvent;
		private void OnBarkEvent(object source, WatchDogEventArgs rea)
		{
			if(BarkEvent!=null)
			{
				this.BarkEvent(source, rea);
			}
		}

		#endregion
		/// <summary>
		///  event will be fired each time the watch dog object 
		///  is going to check the subject object and it is okay
		/// </summary>
		#region Watch dog Idle event
		public event BarkEventHandler IdleEvent;
		private void OnIdleEvent(object source, WatchDogEventArgs rea)
		{
			if(IdleEvent!=null)
			{
				this.IdleEvent(source, rea);
			}
		}

		#endregion

		/// <summary>
		/// Chase is happening when the WatchDog object start checking the process status
		/// </summary>
		private System.Threading.Timer Chase;
		private bool b_IsAwake;
		public ITrainingDog ObjectToBeWatched = null;

		private WatchDog(){}

		/// <summary>
		/// Train the dog to smell the suspected smell until she could differentiate it by barking!
		/// TrainingPeriod is 1/10 of a second, For stupid dogs (slow CPUs!) use larger amount
		/// </summary>
		/// <param name="TrainingPeriod">
		///		The amount of time the dog needs to be trained.
		///		10 means one second and 20 means two seconds and so on
		/// </param>
		public void TrainTheSmell( int TrainingPeriod )
		{
			while((ObjectToBeWatched.SuspectedSmell()!=null) && (TrainingPeriod>0))
			{
				TrainingPeriod--;
				Thread.Sleep(100);
			}
		}

		public WatchDog(ITrainingDog objectToBeWatched)
		{
			ObjectToBeWatched = objectToBeWatched;
			/// Create the delegate that invokes methods for the timer.
			TimerCallback StartChase = new TimerCallback(WakeUpAndChase);
			/// Create a timer that waits one second, then invokes every xxx seconds.
			Chase = new System.Threading.Timer(StartChase , this, Timeout.Infinite, 0);
			
		}

		public void Dispose()
		{
			Chase.Dispose();
			Chase = null;
		}


		public void Sleep()
		{
			Chase.Change( Timeout.Infinite , 0 );
			b_IsAwake = false;
		}

		public void Wakeup( int Period)
		{
			Chase.Change( 1, Period );
			b_IsAwake = true;
		}

		public void Wakeup( int WakeupTime, int Period)
		{
			Chase.Change( WakeupTime, Period );
			b_IsAwake = true;
		}

		private void WakeUpAndChase(Object state)
		{
			WatchDog theDog =(WatchDog)state;
			lock(theDog)
			{
				/// If the smell is suspected!!!
				if(ObjectToBeWatched!=null)
				{
					object o_msg = ObjectToBeWatched.SuspectedSmell();
					if(o_msg!=null)
						OnBarkEvent(this, new WatchDogEventArgs(o_msg));
					else
						OnIdleEvent(this, null);
				}
			}
		}

		public bool IsAwake
		{
			get
			{
				return b_IsAwake;
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior)
Canada Canada
Software development experience since 1993

Skills
- Programming Languages: C# .NET, Delphi, C++, Java and VB
- Development Methodologies (SDLC): RUP, Scrum and XP
- Database: SQL server, Oracle, Apollo database and MS Access.

Educations & Certificates
- Microsoft® Certified Professional (MCP)
- Sun® Certified Java2 Programmer
- B.S. in computer science

Comments and Discussions