I would like to know how I can restart my windows service the way I want it to be.
I have this solution:
private const int RestartTimeout = 60000; // 1 minute
public void Control(string serviceName)
{
service = new ServiceController(serviceName);
}
public bool RestartService()
{
try
{
Control("MyService");
service.Refresh();
if (service.Status != ServiceControllerStatus.Stopped)
{
mytimer.Enabled = false;
service.Stop();
int i = 0;
service.Refresh();
while (service.Status == ServiceControllerStatus.Stopped || service.Status == ServiceControllerStatus.StopPending)
{
i++;
Thread.Sleep(100);
if (i >= RestartTimeout / 100)
{
return false;
}
}
service.Start();
return true;
}
service.Start();
return true;
}
catch (Exception ex)
{
return false;
}
}
I got a timeout error when it reaches the while loop. I guess it's because the service has been stopped?
What I'm really trying to achieve here is to Stop the service and then sleep for 1 minute before starting the service again.
Please help.Thanks!