title: How to stop Windows service programmatically
body: <p>About programming Windows services: how to stop my windows service?</p>
  
  <p>Here is a very simplified example code(C#):</p>
  
  <pre><code>// Here is my service class (MyTestService.cs).
  public class MyTestService:ServiceBase{
  
      // Constructor.
      public MyTestService(){
           this.ServiceName = "My Test Service";
           return;
      }
  };
  
  //  My application class (ApplicationClass.cs).
  public static class ApplicationClass{
  
      // Here is main Main() method.
      public static void Main(){
          // 1. Creating a service instance
          // and running it using ServiceBase.
          MyTestService service = new MyTestService();
          ServiceBase.Run(service);
          // 2. Performing a test shutdown of a service.
          service.Stop();
          Environment.Exit(0);
          return;
      };
  };
  </code></pre>
  
  <p>So: I've just created "My Test Service" started it and stopped. But when I'm looking into my Services.msc - "My Test Service" is continues to running and stops ONLY when I click a "Stop" link. Why? - why <em>service.Stop()</em> command does nothing?</p>
  
  <p>ServiceController.Stop() also does nothing!</p>
  
  <p>How can I stop my service from Main() method?</p>