RIA Services - ReStart of a Window Service - Asked By prince b on 29-Sep-11 02:39 AM

I had a Windows service which deletes the files in a directory after every 1 minute of creation.  So, Once if a file is created and got deleted then again if i add another file in it it was not deleting until i again restart the service

Is there any solution for it
Radhika roy replied to prince b on 29-Sep-11 02:54 AM
Set the service to restart after failure (double click the service in the control panel and have a look around on

those tabs - I forget the name of it). Then, anytime you want the service to restart, just call Environment.Exit(1)

(or any non-zero return) and the OS will restart it for you.
Radhika roy replied to prince b on 29-Sep-11 02:54 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
  {
    // ...
  }
}

 

prince b replied to Radhika roy on 29-Sep-11 03:10 AM
Actually its status is in start only but when i add a file it is not deleting , if i restart it from start status then only it is deleting