Implement the service interface : Service « Windows « C# / CSharp Tutorial






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());
    }
}








29.5.Service
29.5.1.Simplest service
29.5.2.Build a service by subclassing System.ServiceProcess.ServiceBase
29.5.3.Implement the service interface
29.5.4.Service control: start and stop