?? servicecontext.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 + -