Click here to Skip to main content
15,887,776 members
Articles / Programming Languages / C#

Windows Service State Publisher

Rate me:
Please Sign up or sign in to vote.
4.88/5 (12 votes)
13 May 2004CPOL9 min read 70.2K   315   70  
Monitors Windows Services for changes in state using well known design patterns.
using System;
using System.Diagnostics;
using System.ServiceProcess;

namespace kae.ServiceStatePublisher
{
	/// <summary>
	/// Establishes	the	client contract	or interface.  The context delegates state specific	requests to	appropriate	concrete state object.
	/// </summary>
	/// <remarks>
	/// ServiceContext is part of the State Pattern implementation for the ServiceStatePublisher library.
	/// For	more information on	the	state pattern see <i>Design	Patterns: Elements of Reusable Code</i>	by Gamma, et al.
	/// </remarks>
	public class	ServiceContext
	{
		protected ServiceController _controller;
		protected ServiceState _state;

		public ServiceContext()
		{
			State = ServiceStateUnknown.Instance;
			Controller = null;
		}

		public ServiceContext( ServiceController service)
		{
			Controller = service;
		}

		public virtual ServiceController Controller
		{
			get { return _controller; }
			set
			{
				if (_controller != value)
				{
					_controller = value;
					State = QueryServiceState();
				}
			}
		}

		public virtual ServiceState State
		{
			get { return _state; }
			set
			{
				if (_state != value)
					_state = value;
			}
		}

		public void Start()
		{
			State.Start( this);
		}

		public void Stop()
		{
			State.Stop( this);
		}

		public void Pause()
		{
			State.Pause( this);
		}

		public void Continue()
		{
			State.Continue( this);
		}

		public bool CanStart
		{
			get { return State.CanStart( this); }
		}

		public bool CanPause
		{
			get { return State.CanPause( this); }
		}

		public bool CanStop
		{
			get { return State.CanStop( this); }
		}

		protected ServiceState QueryServiceState()
		{
			ServiceState state;

			if (Controller != null)
			{
				switch (Controller.Status)
				{
					case ServiceControllerStatus.ContinuePending:
						state = ServicePendingContinue.Instance;
						break;
					case ServiceControllerStatus.Paused:
						state = ServicePaused.Instance;
						break;
					case ServiceControllerStatus.PausePending:
						state = ServicePendingPause.Instance;
						break;
					case ServiceControllerStatus.Running:
						state = ServiceRunning.Instance;
						break;
					case ServiceControllerStatus.StartPending:
						state = ServicePendingStart.Instance;
						break;
					case ServiceControllerStatus.Stopped:
						state = ServiceStopped.Instance;
						break;
					case ServiceControllerStatus.StopPending:
						state = ServicePendingStop.Instance;
						break;
					default:
						Debug.Assert( false);
						state = ServiceStateUnknown.Instance;
						break;
				}
			}
			else
				state = ServiceStateUnknown.Instance;

			return state;
		}
	}
}

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
United States United States
Dwight D. Eisenhower wrote: "In battle, I believe plans are useless; however, planning is indespensable."

Hmm, perhaps he was an early adopter of agile beliefs?! Wink | ;-)

Comments and Discussions