Service control: start and stop : Service « Windows « C# / CSharp Tutorial






using System;
using System.ServiceProcess;

class MainClass
{
  public static void Main() 
  {
    ServiceController scAlerter = new ServiceController("Alerter");

    Console.WriteLine(scAlerter.DisplayName);
    Console.WriteLine(scAlerter.CanStop);

    scAlerter.Stop();
    Console.WriteLine("Service stopped");
    scAlerter.Start();
    Console.WriteLine("Service started");
  }
}
Alerter
False

Unhandled Exception: System.InvalidOperationException: Cannot stop Alerter service on computer '.'.
---> System.ComponentModel.Win32Exception: The service has not been started
   --- End of inner exception stack trace ---
   at System.ServiceProcess.ServiceController.Stop()
   at MainClass.Main()








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