Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop Windows service programmatically

Tags:

About programming Windows services: how to stop my windows service?

Here is a very simplified example code(C#):

// 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;     }; }; 

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 service.Stop() command does nothing?

ServiceController.Stop() also does nothing!

How can I stop my service from Main() method?

like image 654
roman_lenko Avatar asked May 01 '13 11:05

roman_lenko


People also ask

How to stop a Windows service programmatically?

Performing a test shutdown of a service. service. Stop(); Environment. Exit(0); return; }; };

How do you stop a Windows service that won't stop?

The easiest way to stop a stuck service is to use the built-in taskkill command-line tool. First of all, you need to find the PID (process identifier) of the service. As an example, let's take the Windows Update service. Its system name is wuauserv (you can check the name in the service properties in the services.


2 Answers

The Stop-function sends a stop-signal. It does not wait till the signal is received and processed.

You will have to wait till the Stop-signal has done it's work. You can do that by calling WaitForStatus:

service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped); 

See for more info: http://msdn.microsoft.com/nl-nl/library/system.serviceprocess.servicecontroller.waitforstatus(v=vs.71).aspx

Environment.Exit is a nasty one. DO NOT USE IT! It aborts your application the hard way, without performing any cleanup in finally blocks, without calling finalizer methods by the GC, it terminates all other forground threads, etc. I can imagine that your application is aborted before the stop-signal even left your application.

like image 65
Martin Mulder Avatar answered Sep 21 '22 04:09

Martin Mulder


I am using following functions in my project

    public static ServiceController GetService(string serviceName)     {         ServiceController[] services = ServiceController.GetServices();         return services.FirstOrDefault(_ => Contracts.Extensions.CompareStrings(_.ServiceName, serviceName));     }      public static bool IsServiceRunning(string serviceName)     {         ServiceControllerStatus status;         uint counter = 0;         do         {             ServiceController service = GetService(serviceName);             if (service == null)             {                 return false;             }              Thread.Sleep(100);             status = service.Status;         } while (!(status == ServiceControllerStatus.Stopped ||                    status == ServiceControllerStatus.Running) &&                  (++counter < 30));         return status == ServiceControllerStatus.Running;     }      public static bool IsServiceInstalled(string serviceName)     {         return GetService(serviceName) != null;     }      public static void StartService(string serviceName)     {         ServiceController controller = GetService(serviceName);         if (controller == null)         {             return;         }          controller.Start();         controller.WaitForStatus(ServiceControllerStatus.Running);     }      public static void StopService(string serviceName)     {         ServiceController controller = GetService(serviceName);         if (controller == null)         {             return;         }          controller.Stop();         controller.WaitForStatus(ServiceControllerStatus.Stopped);     } 
like image 32
Uzzy Avatar answered Sep 24 '22 04:09

Uzzy