C# .NET - Windows Services - Asked By prince b on 29-Sep-11 07:56 AM

When I install the Service , I manually starting the Service for the first time.....

Can I make it to start automatically after the installation of it
smr replied to prince b on 29-Sep-11 08:02 AM
HI

In your Installer class, add a handler for the AfterInstall event. You can then call the ServiceController in the event handler to start the service.
 
public ServiceInstaller()
{
  //... Installer code here
  this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);
}
 
void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
{
  ServiceController sc = new ServiceController("ServiceName");
  sc.Start();
}
smr replied to prince b on 29-Sep-11 08:03 AM
you can do in this way also

Add the following class to your project.
 
using System.ServiceProcess; 
 
class ServInstaller : ServiceInstaller
{
  protected override void OnCommitted(System.Collections.IDictionary savedState)
  {
    ServiceController sc = new ServiceController("YourServiceNameGoesHere");
    sc.Start();
  }
}
 
The Setup Project will pick up the class and run your service after the installer finishes
prince b replied to smr on 29-Sep-11 08:20 AM
thanks dude...... it worked...
Reena Jain replied to prince b on 29-Sep-11 08:45 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