C# .NET - Starting windows service - Asked By Az D on 09-May-12 05:41 AM

I have a windows service which will download files from FTP , Process & save them.
The code works successfull, now I removed the below code (debug mode)

Myservice service = new Myservice();

service.ManualStart();


//Now I install my service , start the installed service. It do not work.

 

//Can some tell me what I’m missing in my service.

kalpana aparnathi replied to Az D on 09-May-12 05:58 AM
HI,

go to start>run>services.msc It lists all the services available and gives you the option to enable/disable, and set startup options.

REGARDS,
Somesh Yadav replied to Az D on 09-May-12 07:35 AM

Start, Stop and Restart Windows Service [C#]

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.

[C#]
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.

[C#]
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.

[C#]
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
  {
    // ...
  }
}

Az D replied to Somesh Yadav on 10-May-12 02:30 AM
Hey all

The problem is solved.
1. service was not built in release mode.
2. project setup file was not built properly.


Thanks,