Reputation:
I have a windows service manager, I try to stop the service by the manager. However I got the exception:
Time out has expired and the operation has not been completed
private static void StopService()
{
if (!IsInstalled()) return;
try
{
CFEServiceController c = new CFEServiceController();
c.StopService(ServiceName, 500);
}
catch (Exception ex)
{
Console.WriteLine("Error in Stop service " + ex.Message);
}
}
And:
public void StopService(string serviceName, int timeoutMilliseconds)
{
using (ServiceController service = new ServiceController(serviceName))
{
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
if (service.Status == ServiceControllerStatus.Running)
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
Console.WriteLine("Stop service " + serviceName+" ");
}
}
catch (Exception ex)
{
Console.WriteLine("Error when stop service in code StopService " + ex.Message); // here I got the exception
}
}
}
Upvotes: 3
Views: 9393
Reputation: 767
You could also try open
"Event Viewer" -> "Windows Logs" -> "Application"
There could be Error Level message that could give you a clue if increasing/unset the number of timeout doesn't help as answered by @gleng.
Upvotes: 2
Reputation: 6304
The problem is because of this:
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
This is happening because your service has not stopped within your timeout
. You should either set your timeout higher, or don't set a timeout at all.
Upvotes: 3