gpt4 book ai didi

c# - 超时已过且服务管理器中的操作尚未完成

转载 作者:行者123 更新时间:2023-11-30 20:53:41 25 4
gpt4 key购买 nike

我有一个 Windows 服务管理器,我试图通过管理器停止该服务。但是我得到了异常(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);
}
}

和:

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
}
}
}

最佳答案

问题是因为这个:

service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

发生这种情况是因为您的服务没有在您的超时 内停止。您应该将超时设置得更高,或者根本不设置超时。

关于c# - 超时已过且服务管理器中的操作尚未完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19775551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com