我有一个 Windows 服务管理器,我尝试通过管理器停止服务。但是我得到了例外:
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);
}
}
和:
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
}
}
}