How to Restart a Windows Service programatically

By | March 29, 2010

Do you ever wondered how to stop and start a service programatically? I had created a simple library to do it so you can use it either on your desktop, console or web application.  All you need is the ServiceProcess Class, this class represents a Windows service and allows you to connect to a running or stopped service, manipulate it, or get information about it.

I had created 6 methods to start with.

CheckService –  To check whether the service is running, stopped or not installed.
StartService – To start the specific service.
StopService – To stop the specific service.
PauseService – To pause the specific service.
ContinueService – To start the paused service.
RestartService – To restart the specific service.

When you create your project remember to add the System.ServiceProcess reference to the project by right clicking on your project and choosing add reference as such

Add Reference

And go to the .Net tab as it is a .Net Library and choose System.ServiceProcess and click OK

System.ServiceController Reference

Now here it the code:

using System;
using System.Web;
using System.ServiceProcess;

public class clsManageService
{
 public static string CheckService(string sServiceName)
 {
 try
 {
 ServiceController oService = new ServiceController();
 oService.ServiceName = sServiceName;

 switch (oService.Status)
 {
 //The service has been paused and is about to continue.
 case ServiceControllerStatus.ContinuePending:
 return "Service is continuing";
 //The service is paused.
 case ServiceControllerStatus.Paused:
 return "Service is paused";
 //The service is in the process of pausing.
 case ServiceControllerStatus.PausePending:
 return "Service is pasuing";
 //The service is in the process of starting.
 case ServiceControllerStatus.StartPending:
 return "Service is starting";
 //The service is not running.
 case ServiceControllerStatus.Stopped:
 return "Service is stopped";
 //The service is in the process of stopping.
 case ServiceControllerStatus.StopPending:
 return "Service is stopping";
 //The service is running.
 default:
 return "Service is running";
 }
 }
 catch (Exception ex)
 {
 return "Service not Installed : " + ex.Message.ToString(); ;
 }

 }
 public static void StartService(string sServiceName, int iTimeOutMS, out string sMessage)
 {
 sMessage = string.Empty;
 try
 {
 ServiceController oService = new ServiceController(sServiceName);

 TimeSpan oTimeSpan = TimeSpan.FromMilliseconds(iTimeOutMS);

 oService.Start();
 oService.WaitForStatus(ServiceControllerStatus.Running, oTimeSpan);
 }
 catch (Exception ex)
 {
 sMessage = ex.Message.ToString();
 }
 }
 public static void StopService(string sServiceName, int iTimeOut, out string sMessage)
 {
 sMessage = string.Empty;
 try
 {
 ServiceController oService = new ServiceController(sServiceName);

 if (oService.CanStop)
 {
 TimeSpan oTimeSpan = TimeSpan.FromMilliseconds(iTimeOut);

 oService.Stop();
 oService.WaitForStatus(ServiceControllerStatus.Stopped, oTimeSpan);
 }
 else
 {
 sMessage = "Service cannot be Stopped";
 }
 }
 catch (Exception ex)
 {
 sMessage = ex.Message.ToString();
 }
 }
 public static void PauseService(string sServiceName, int iTimeOut, out string sMessage)
 {
 sMessage = string.Empty;
 try
 {
 ServiceController oService = new ServiceController(sServiceName);

 if (oService.CanPauseAndContinue)
 {
 TimeSpan oTimeSpan = TimeSpan.FromMilliseconds(iTimeOut);

 oService.Pause();
 oService.WaitForStatus(ServiceControllerStatus.Stopped, oTimeSpan);
 }
 else
 {
 sMessage = "Service cannot be Paused";
 }
 }
 catch (Exception ex)
 {
 sMessage = ex.Message.ToString();
 }
 }
 public static void ContinueService(string sServiceName, int iTimeOut, out string sMessage)
 {
 sMessage = string.Empty;
 try
 {
 ServiceController oService = new ServiceController(sServiceName);

 TimeSpan oTimeSpan = TimeSpan.FromMilliseconds(iTimeOut);

 oService.Continue();
 oService.WaitForStatus(ServiceControllerStatus.Stopped, oTimeSpan);

 }
 catch (Exception ex)
 {
 sMessage = ex.Message.ToString();
 }
 }

 public static void RestartService(string sServiceName, int iTimeOut, out string sMessage)
 {
 sMessage = string.Empty;
 try
 {
 ServiceController oService = new ServiceController(sServiceName);

 int iStartTime = Environment.TickCount;
 TimeSpan oTimeSpan = TimeSpan.FromMilliseconds(iTimeOut);

 //Stop the Service
 oService.Stop();
 oService.WaitForStatus(ServiceControllerStatus.Stopped, oTimeSpan);

 int iEndTime = Environment.TickCount;
 oTimeSpan = TimeSpan.FromMilliseconds(iTimeOut - (iEndTime - iStartTime));

 //Start the Service Again
 oService.Start();
 oService.WaitForStatus(ServiceControllerStatus.Running, oTimeSpan);
 }
 catch (Exception ex)
 {
 sMessage = ex.Message.ToString();
 }
 }

}
Recommended

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.