在Windows服务上处理停止/暂停请求期间的延迟 [英] Handling delays during stop/pause requests on Windows service

查看:45
本文介绍了在Windows服务上处理停止/暂停请求期间的延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows服务类,它是从 ServiceBase 派生的,该类使用 System.Timers.Timer 定期运行代码. OnStop OnPause 的处理程序使用一些带有计时器线程的信号来检查计时器是否仍在运行并等待其结束.

I have a Windows service class deriving from ServiceBase that uses a System.Timers.Timer to run code at frequent intervals. The handlers for OnStop and OnPause use some signalling with the timer thread to check if the timer is still running and waits for it to finish.

在这种情况下是否存在处理延迟错误的推荐方法,例如处理程序是否在等待时间过长而导致计时器线程停止?

Is there a recommended way of handling delay errors in this situation, such as if the handler is waiting for an unacceptably long time for the timer thread to stop?

我应该继续等待直到SCM放弃(但这将使服务进入一种状态,在这种状态下,您只能对进程执行任何操作,除非杀死进程,并且有时重新启动是重新启动它的唯一方法)?还是我可能抛出一个异常(这会让它保持类似的状态,还是只是中止停止/暂停请求)?

Should I just continue waiting until the SCM gives up (but that will put the service into a state where you can't do anything with the service except kill the process, and sometimes a reboot is the only way to restart it)? Or I could throw an exception perhaps (would this leave it in a similar state, or just abort the stop/pause request)?

如果我可以拒绝暂停/停止请求并使SCM处于用户可以再次尝试暂停/停止的状态,则我会更喜欢它.我可以中止计时器线程,但有时它会锁定在其他资源上,而我希望它等到完成为止,并且只记录一个错误,管理员可以稍后尝试停止.

I'd prefer it if I can reject the request to pause/stop and have the SCM leave it in a state where the user can try to pause/stop again. I could abort the timer thread but sometimes it's locked on other resources that I'd rather it would wait until completion, and just have an error logged with the administrator able to attempt a stop later.

推荐答案

您可以使用

You can use the RequestAdditionalTime method to inform the SCM that you need more time to stop/pause your service.

这篇关于在Windows服务上处理停止/暂停请求期间的延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆