Csharp/C Sharp by API/System.ServiceProcess/ServiceBase
extends ServiceBase
using System;
using System.Collections;
using System.ruponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
public class Service1 : System.ServiceProcess.ServiceBase
{
public Service1()
{
this.ServiceName = "MyFirstService";
}
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
protected override void OnStart(string[] args)
{
EventLog.WriteEntry( "Hello from MyFirstService" );
}
protected override void OnStop()
{
EventLog.WriteEntry( "Goodbye from MyFirstService" );
}
}
ServiceBase.CanHandleSessionChangeEvent
using System;
using System.Timers;
using System.ServiceProcess;
class MyService : ServiceBase
{
public MyService()
{
ServiceName = "My Service";
AutoLog = true;
CanStop = true;
CanPauseAndContinue = true;
CanHandleSessionChangeEvent = true;
}
protected override void OnStart(string[] args)
{
EventLog.WriteEntry("MyService Service starting. ");
}
protected override void OnStop()
{
EventLog.WriteEntry("MyService Service stopping...");
}
protected override void OnPause()
{
EventLog.WriteEntry("MyService Service pausing...");
}
protected override void OnContinue()
{
EventLog.WriteEntry("MyService Service resuming...");
}
protected override void OnSessionChange(SessionChangeDescription change)
{
EventLog.WriteEntry("MyService Session change..." +
change.Reason);
}
public static void Main()
{
ServiceBase.Run(new MyService());
}
}
ServiceBase.Run
using System;
using System.Collections;
using System.ruponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
public class Service1 : System.ServiceProcess.ServiceBase
{
public Service1()
{
this.ServiceName = "MyRealService";
}
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
protected override void OnStart(string[] args)
{
int x = 1;
}
protected override void OnStop()
{
}
}