www.pudn.com > OracleServiceManager.zip > ServiceRunning.cs


using System; 
using System.Diagnostics; 
using System.ServiceProcess; 
 
namespace kae.ServiceStatePublisher 
{ 
	///  
	/// ServiceRunning is a concrete ServiceState representing the  
	/// ServiceController state of running.  As a singleton, only  
	/// one instance of this class is needed for the application. 
	///  
	public class ServiceRunning : ServiceState 
	{ 
		private static ServiceRunning _instance; 
 
		static ServiceRunning() 
		{ 
			lock (typeof(ServiceRunning)) 
			{ 
				if (_instance == null) 
					_instance = new ServiceRunning(); 
			} 
		} 
 
		public static ServiceRunning Instance 
		{ 
			get { return _instance; } 
		} 
 
		public override void Stop( ServiceContext context) 
		{ 
			try 
			{ 
				context.Controller.Stop(); 
				context.Controller.WaitForStatus( ServiceControllerStatus.Stopped, new TimeSpan(0,0,30)); 
				ChangeState( context, ServiceStopped.Instance); 
			} 
			catch (TimeoutException te) 
			{ 
				LogError( te.Message, EventLogEntryType.Warning); 
				throw; 
			} 
		} 
 
		public override void Pause( ServiceContext context) 
		{ 
			try 
			{ 
				context.Controller.Pause(); 
				context.Controller.WaitForStatus( ServiceControllerStatus.Paused, new TimeSpan(0,0,30)); 
				ChangeState( context, ServicePaused.Instance); 
			} 
			catch (TimeoutException te) 
			{ 
				LogError( te.Message, EventLogEntryType.Warning); 
				throw; 
			} 
		} 
 
		public override string Status 
		{ 
			get { return "Running"; } 
		} 
 
		public override bool IsRunning 
		{ 
			get { return true; } 
		} 
	} 
}