www.pudn.com > OracleServiceManager.zip > ServiceState.cs
using System;
using System.Diagnostics;
using System.ServiceProcess;
namespace kae.ServiceStatePublisher
{
///
/// Defines an interface for encapsulating the behavior associated with a particular state of the Context.
///
///
/// 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 Design Patterns: Elements of Reusable Code by Gamma, et al.
///
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();
}
}
}