⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 servicecontext.cs

📁 Oracle Service Manager
💻 CS
字号:
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;
		}
	}
}

⌨️ 快捷键说明

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