ServiceController Class in Windows Service

This articles explains the ServiceController. The ServiceController component is used to fetch the installed services and manipulate the state of the service. We can start and stop a service using the Service Controller and can pause and continue its functioning.

A Windows Service is formally known as am NT Service that creates a long-running executable application that runs on a Windows session. Windows Services can be started automatically when the computer boots, we can pause and restart too. These services have no user interface, this features makes the service ideal for use on a server. We can easily create a service by a Visual Studio project.

What SericeController class is

A ServiceController class represents a Windows service that allows us to connect to a running or stopped service. We can also change the state of the service and get information about the service. A ServiceController class is connected with our already installed class and change the behavior of our existing services. When we create an instance of the Service controller class, we set the property that interacts with our installed Windows Service. We can understand the ServiceController as an administartive purpose tool that mainly handle the Service Control Manager (SCM).

Example

Let's try to understand it with an example.

If we need to fetch all the installed services in our computer then we can use the following:

  1. using System;  
  2. using System.ServiceProcess;  
  3. using System.Diagnostics;  
  4. using System.Threading;  
  5.   
  6. namespace ServiceControllerDemo  
  7. {  
  8.     class Program  
  9.     {  
  10.         public enum ServiceCustomCommands  
  11.         {  
  12.             StopWorker = 128,  
  13.             RestartWorker,  
  14.             CheckWorker  
  15.         };  
  16.         static void Main(string[] args)  
  17.         {  
  18.             ServiceController[] objService;  
  19.             objService = ServiceController.GetServices();  
  20.         }  
  21.     }  
  22. }  
ServiceControllerOUtput2

After exploring the Service.

ServiceControllerOUtput1

Now, let's try to control a service. In this example we are only getting the status of the service, but we can also manipulate the service like start, pause and and so on. 

  1. using System;  
  2. using System.ServiceProcess;  
  3. using System.Diagnostics;  
  4. using System.Threading;  
  5.   
  6. namespace ServiceControllerDemo  
  7. {  
  8.     class Program  
  9.     {  
  10.         public enum ServiceCustomCommands  
  11.         {  
  12.             StopWorker = 128,  
  13.             RestartWorker,  
  14.             CheckWorker  
  15.         };  
  16.         static void Main(string[] args)  
  17.         {  
  18.             ServiceController[] objService;  
  19.             objService = ServiceController.GetServices();  
  20.             foreach (ServiceController SController in objService)  
  21.             {  
  22.                 if (SController.ServiceName == "AdobeARMservice")  
  23.                 {  
  24.                     ServiceController sc = new ServiceController("Adobe Acrobat Update Service");  
  25.                     Console.WriteLine("Status = " + sc.Status);  
  26.                     Console.WriteLine("Pause and Continue ? = " + sc.CanPauseAndContinue);  
  27.                     Console.WriteLine("ShutDown ? = " + sc.CanShutdown);  
  28.                     Console.WriteLine("Stop ? = " + sc.CanStop);  
  29.   
  30.   
  31.   
  32.                 }  
  33.             }  
  34.   
  35.         }  
  36.     }  
  37. }  
FinalOutputOfService1

Summary

In this article we saw the benefits of the ServiceController class and its uses. I hope you like this article. 


Similar Articles