servicestate.cs

来自「Oracle Service Manager」· CS 代码 · 共 103 行

CS
103
字号
using System;
using System.Diagnostics;
using System.ServiceProcess;

namespace kae.ServiceStatePublisher
{
	/// <summary>
	/// Defines an interface for encapsulating the behavior associated with a particular state of the Context.
	/// </summary>
	/// <remarks>
	/// ServiceState is implemented as an abstract class, rather than an interface.  This choice provides valuable assistance supporting default behavior.
	/// Since the class is abstract, a client cannot use this class on its own. 
	/// For more information on the state pattern see <i>Design Patterns: Elements of Reusable Code</i> by Gamma, et al.
	/// </remarks>
	public abstract class ServiceState
	{
		private const string SSP_SOURCE = "ServiceStatePublisher";
		private const string SSP_EVENTLOG = "Application";

		public ServiceState() { }

		public virtual void Start( ServiceContext context) { }
		public virtual void Stop( ServiceContext context) { }
		public virtual void Pause( ServiceContext context) { }
		public virtual void Continue( ServiceContext context) { }

		protected void ChangeState( ServiceContext context, ServiceState state)
		{
			context.State = state;
		}

		public override string ToString()
		{
			return String.Empty;
		}

		public virtual string Status
		{
			get { return String.Empty; }
		}

		public virtual bool IsRunning
		{
			get { return false; }
		}

		public virtual bool IsStopped
		{
			get { return false; }
		}

		public virtual bool IsPaused
		{
			get { return false; }
		}

		public virtual bool IsChanging
		{
			get { return false; }
		}

		public virtual bool CanStart( ServiceContext context)
		{
			bool canStart = false;
			if (context.Controller != null)
				canStart = !(IsRunning | IsChanging);

			return canStart;
		}

		public virtual bool CanPause( ServiceContext context)
		{
			bool canPause = false;
			if (context.Controller != null && context.Controller.CanPauseAndContinue)
				canPause = !(IsPaused | IsChanging);

			return canPause;
		}

		public virtual bool CanStop( ServiceContext context)
		{
			bool canStop = false;
			if (context.Controller != null)
				canStop = !(IsStopped | IsChanging);

			return canStop;
		}

		protected virtual void LogError( string message, EventLogEntryType entryType)
		{
			if (!EventLog.SourceExists( SSP_SOURCE))
				EventLog.CreateEventSource( SSP_SOURCE, SSP_EVENTLOG);

			EventLog sspLog = new EventLog();
			sspLog.Source = SSP_SOURCE;

			sspLog.WriteEntry( message, entryType);

			sspLog.Close();
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?