C# .NET - Restart Window Service in C# - Asked By Xor Power on 06-Apr-11 01:33 AM

Hi,

I wanted to restart my windows service programmatically. How to do that?


Let me know if more info required from my end

Thanks for all your inputs.
Reena Jain replied to Xor Power on 06-Apr-11 01:43 AM
hi,

This example shows how to start, stop and restart a windows service programmatically in C#.

Start service

The following method tries to start a service specified by a service name. Then it waits until the service is running or a timeout occurs.

public static void StartService(string serviceName, int timeoutMilliseconds)
{
  ServiceController service = new ServiceController(serviceName);
  try
  {
  TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
 
  service.Start();
  service.WaitForStatus(ServiceControllerStatus.Running, timeout);
  }
  catch
  {
  // ...
  }
}
 

Stop service
 The following method tries to stop the specified service and it waits until the service is stopped or a timeout occurs.
public static void StopService(string serviceName, int timeoutMilliseconds)
{
  ServiceController service = new ServiceController(serviceName);
  try
  {
  TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
  
  service.Stop();
  service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
  }
  catch
  {
  // ...
  }
}
Restart service

This method combinates both previous methods. It tries to stop the service (and waits until it's stopped) then it begins to start the service (and waits until the service is running). The specified timeout is used for both operations together.

public static void RestartService(string serviceName, int timeoutMilliseconds)
{
  ServiceController service = new ServiceController(serviceName);
  try
  {
  int millisec1 = Environment.TickCount;
  TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
 
  service.Stop();
  service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
 
  // count the rest of the timeout
  int millisec2 = Environment.TickCount;
  timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2-millisec1));
 
  service.Start();
  service.WaitForStatus(ServiceControllerStatus.Running, timeout);
  }
  catch
  {
  // ...
  }
}

Hope this will help you
Xor Power replied to Reena Jain on 06-Apr-11 01:46 AM
thanks for the input, but can you tell me where to write the code to restart the service?
Anoop S replied to Xor Power on 06-Apr-11 01:49 AM

check out these links for examples

http://www.aspfree.com/c/a/C-Sharp/Timer-Objects-in-Windows-Services-with-C-sharp-dot-NET/3/

http://www.vbdotnetheaven.com/UploadFile/mgold/ApplicationScheduler11112005001112AM/ApplicationScheduler.aspx?ArticleID=7f292e7f-10cd-4577-93f3-8a0d63dfe2e9

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vboricreatingconfiguringwindowsserviceapplications.asp

http://www.aspfree.com/c/a/C-Sharp/Timer-Objects-in-Windows-Services-with-C-sharp-dot-NET/1/

Jitendra Faye replied to Xor Power on 06-Apr-11 01:57 AM
Restarting window service is combination of stoping and starting window service,

use this code for restarting window service-

public static void RestartService(string serviceName, int timeoutMilliseconds)
{
    ServiceController serviceObj =
new ServiceController(serviceName);
    try
    {
      int ms1 = Environment.TickCount;
      TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
      serviceObj .Stop();
      serviceObj .WaitForStatus(ServiceControllerStatus.Stopped, timeout);

      // count the rest of the timeout
      int ms2 = Environment.TickCount;
      timeout =
TimeSpan.FromMilliseconds(timeoutMilliseconds - (ms2-ms1));
      serviceObj .Start();
      serviceObj .WaitForStatus(ServiceControllerStatus.Running, timeout);
    }
    catch
    {
      // CODE FOR CATCHING ERROR
    }
}
USE THIS CODE AND LET ME KNOW.

Xor Power replied to Jitendra Faye on 06-Apr-11 02:03 AM
thanks for all your inputs. but i am NOT sure, where am i suppose to write the code to restart the services?
Jitendra Faye replied to Xor Power on 06-Apr-11 02:14 AM
In service only you have to write this code. after writing this code in service you cam call RestartService() method
for restarting this service.
Reena Jain replied to Xor Power on 06-Apr-11 02:41 AM
hi,

these are the functions you can put in anywhere in service. You can call it according to your choice or condition requirement.

Hope this will help you
Xor Power replied to Jitendra Faye on 06-Apr-11 02:43 AM
@Jitendra Faye: Won't that be included in Installer File of that windows service? Because the code to start & stop the service is written there