Windows服务可以花费几个小时才能优雅地关闭吗?

发布于 2024-12-05 07:05:41 字数 418 浏览 0 评论 0原文

我们有一个 .NET Windows 服务,它以事务方式将大量高度关键的数据从 A 转移到 B。 我们还需要确保服务中使用的所有外部组件均未正确分配,并且在关闭服务本身之前已清理所有内容。 这可能需要几个小时!原因是该服务需要等待外部组件的回调,该回调会在 2、3 或 4 小时后到达。

  1. Windows 是否可能需要等待很长时间才能获得服务 优雅地关闭?
  2. 服务中是否有选项可以让我决定在什么情况下会发生什么 该服务正在被操作系统关闭,例如阻止 完全关闭?
  3. 另外,作为另一种情况,如果服务器需要,会发生什么 重启?可以等待几个小时才能获得服务吗?
  4. 操作系统在终止服务之前等待服务的时间是否有限制?

We have a .NET Windows service that shovels a lot of highly critical data from A to B in a transactional manner.
We also need to make sure that all external compontents used in the service are unallocated correctly and everything cleaned up before shutting down the service itself. This can take hours! The reason for this is that the service needs to wait on an external component's callback, which arrives 2, 3, or 4 hours later.

  1. is it possible for Windows to wait so long for a service to
    shutdown gracefully?
  2. are there options in a service where I can dictate what happens when
    the service is being shutdown by the operating system, e.g. prevent
    the shutdown altogether?
  3. also, as another scenario, what happens if the server needs to
    reboot? Can it wait hours for the service?
  4. Is there a limit on how long the OS will wait on the service before killing it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏夜暖风 2024-12-12 07:05:42

您可以使用 canStop,canshutdown,canshutdown,canhandlepoferevent 当计算机关闭并充分响应时,要通知。

使用servicebase.requestditionaltime方法请求终止线程的额外时间:

protected override void OnShutdown() 
{
    base.RequestAdditionalTime(MaxTimeout);
    serviceCore.OnShutdown();
    Stop(); 
}

如果您的onshutdown()块超过20秒(默认值存储在注册表中hkey_local_machine \ system \ currentcontrolset \ currentcontrolset \ current controlset \ waittokillserviceTime \ waittokillservicetime out),则将标记为AS。无反应和杀害。

我推荐的BCL团队有一篇很好的博客文章。

You can use CanStop, CanShutdown, CanHandlePowerEvent to be notified when the computer is shutting down and respond adequately.

Use the ServiceBase.RequestAdditionalTime method to request additional time to terminate your thread:

protected override void OnShutdown() 
{
    base.RequestAdditionalTime(MaxTimeout);
    serviceCore.OnShutdown();
    Stop(); 
}

If your OnShutdown() blocks for longer than 20 seconds (default value stored in the registry HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\WaitToKillServiceTimeout), your service will be marked as unresponsive and killed.

There is a good blog post from the BCL team on that subject that I recommend.

江城子 2024-12-12 07:05:42

是的,服务可以停滞直到完成,但是应该吗?注册表中有超时值,什么是OS?

我认为您需要将回调从其他组件中解除,或将缓冲区数据解除到磁盘并发送较小的片段。强力尺寸会发生什么?还是有人厌倦了等待并关闭?

如果数据至关重要,则在内存之外管理它,并使过程可重新启动。这将解决您的长时间关闭过程。同样,一种对进度组件进行轮询的方法,取消对阻塞的依赖。

Yes a service can stall shutdown until it completes, but should it? There are timeout values in the registry for this, what's the OS?

I think you need to decouple the callback from the other component, or buffer data to disk and send in smaller fragments. What happens in a powercut? Or if someone gets fed up waiting and switches off?

If the data is critical manage it outside of memory, and make the process restartable. This will solve your lengthly shutdown process. Likewise a way to poll the component for progress, takes away the dependency on blocking.

乜一 2024-12-12 07:05:42

您不能在 OnShutdown() 内使用 RequestAdditionalTime(MaxTimeout) ...

You can not use RequestAdditionalTime(MaxTimeout) inside OnShutdown() ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文