C# Windows 服务等待处理停止请求的最长时间是多少以及如何请求额外的时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13454054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:37:01  来源:igfitidea点击:

what is the maximum time windows service wait to process stop request and how to request for additional time

c#.netwindows-services

提问by Imran Rizvi

I have written a windows service in c# that process a lot data. when we stop it try for sometime 20/30 seconds and then throws exception.

我用 c# 编写了一个处理大量数据的 Windows 服务。当我们停止它时,尝试 20/30 秒然后抛出异常。

I want to implement ServiceBase.RequestAdditionalTime() in OnStop event.

我想在 OnStop 事件中实现 ServiceBase.RequestAdditionalTime()。

I want to know the exact timeout after which windows service throws the exception, so that I can request additional time just before it.

我想知道 Windows 服务抛出异常的确切超时时间,以便我可以在它之前请求额外的时间。

I searched but did not find this default stop timeout value.

我搜索但没有找到这个默认的停止超时值。

采纳答案by Imran Rizvi

I wrote the following code to achieve it.

我编写了以下代码来实现它。

protected override void OnStop()
{
  int timeout = 10000;
  var task = Task.Factory.StartNew(() => MyTask());
  while (!task.Wait(timeout))
  {
      RequestAdditionalTime(timeout);
  }
}

The above code starts a Task in Parallel to the main thread (Task start running immediately), next line is to check if task is completed or not every 10 seconds and if it is not completed it requests additional 10 seconds and keep checking till task get completed.

上面的代码启动一个与主线程并行的任务(任务立即开始运行),下一行是每 10 秒检查一次任务是否完成,如果未完成则请求额外的 10 秒并继续检查直到任务获得完全的。

回答by Saddam Abu Ghaida

by default I believe it is 12000 milliseconds, to change it you need to access registry HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\WaitToKillServiceTimeout and change the value

默认情况下,我认为它是 12000 毫秒,要更改它,您需要访问注册表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\WaitToKillServiceTimeout 并更改值

but you can define your own time out if you want to start it or stop it programming here you define your own time out for starting

但是你可以定义你自己的超时,如果你想启动它或停止它编程在这里你定义你自己的启动超时

TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);

and here you define your own time out for stopping

在这里您可以定义自己的停止时间

 TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
 service.Stop();
 service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

回答by spender

It's set in the registry on subkey:

它在子项的注册表中设置:

 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control

with string value WaitToKillServiceTimeout. If not defined, it defaults to 20000 (ms). On my machine it seems to be set to 12000 (12s). I have never touched it.

与字符串值WaitToKillServiceTimeout。如果未定义,则默认为 20000(毫秒)。在我的机器上,它似乎设置为 12000(12 秒)。我从来没有碰过它。

回答by Simon Richter

Just always perform the RequestAdditionalTime, with the maximum time you expect your service to need for shutdown. It is not an error to finish earlier than predicted.

只需始终执行RequestAdditionalTime,并使用您期望服务关闭所需的最长时间。早于预期完成并不是错误。

回答by Chris Kline

Different settings for OS restart

操作系统重启的不同设置

Though a number of people have mentioned the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\WaitToKillServiceTimeoutregistry key, according to this "Service Control Handler" article from Microsoftthat registry entry onlycontrols the max amount of time a service can take to shut down when Windows itself is being shut down or restarted:

尽管很多人都提到了HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\WaitToKillServiceTimeout注册表项,但根据Microsoft 的这篇“服务控制处理程序”文章,注册表项控制在 Windows 本身被关闭或重新启动时服务可以关闭的最长时间:

<...> to prevent a service from stopping shutdown, there is a limit to how long the service controller waits. If the service is being shut down through the Services snap-in, the limit is 125 seconds. If the operating system is rebooting, the time limit is specified in the WaitToKillServiceTimeoutvalue <...>

<...> 为防止服务停止关闭,服务控制器等待的时间是有限制的。如果通过服务管理单元关闭服务,则限制为 125 秒。如果操作系统正在重新启动,则时间限制在WaitToKillServiceTimeout值 <...> 中指定

If Windows is not in the process of restarting or shutting down, then the defaultamount of time Windows will wait for a service to shut down is 30 seconds. However, applications can make requests for additional time, which will be honored up to 125 seconds total (summed across all requests).

如果 Windows 不在重新启动或关闭过程中,则Windows 等待服务关闭的默认时间为 30 秒。但是,应用程序可以请求额外的时间,总共最多 125 秒(所有请求的总和)。

On Windows Server 2003 and later, this default timeout can be changedvia the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ServicesPipeTimeoutregistry key, as described in this Microsoft support article(and this ServerFault question). It's not clear if this applies to Windows 7/8/10, as the article only mentions server versions.

在 Windows Server 2003 及更高版本上,可以通过HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ServicesPipeTimeout注册表项更改此默认超时如此 Microsoft 支持文章(以及此 ServerFault 问题)中所述。目前尚不清楚这是否适用于 Windows 7/8/10,因为该文章仅提及服务器版本。

If a restart/shutdown has been initiatedon the machine, the WaitToKillServiceTimeoutregistry key value (if present) specifies the maximum amount of time Windows will allow the application will be allowed, overriding the OS default.

如果已在计算机上启动重新启动/关闭,则WaitToKillServiceTimeout注册表项值(如果存在)指定 Windows 允许应用程序被允许的最长时间,覆盖操作系统默认值。

Presumably this is so that applications cannot arbitrarily delay shutdown beyond the default (or what the administrator specified via the WaitToKillServiceTimeoutregistry entry).

大概这是为了使应用程序不能在默认值(或管理员通过WaitToKillServiceTimeout注册表项指定的值)之外任意延迟关闭。