c# - 如何通过 C# 暂停和继续 Windows 服务

标签 c# windows-services

我用 c# 4 编写了一个 Windows 服务并覆盖了它的一些方法,比如 OnStart() 和 OnStop() 和 OnPause() 和 OnContinue() 和 OnShutdown()。

我可以启动或停止此 Windows 服务,但无论是通过编程还是在 services.msc 中,我都无法暂停或继续它。换句话说,Start 和Stop 是Enable,Pause 和Resume Options 是Disable。

enter image description here

什么是问题?

最佳答案

您需要设置 CanPauseAndContinue 属性也为真。这记录在 OnContinue 中:

OnContinue is expected to be overridden when the CanPauseAndContinue property is true.

If CanPauseAndContinue is false, the SCM will not pass Pause or Continue requests to the service, so the OnPause and OnContinue methods will not be called even if they are implemented. In the SCM, the Pause and Continue controls are disabled when CanPauseAndContinue is false.

关于c# - 如何通过 C# 暂停和继续 Windows 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16249428/

相关文章:

vb.net - System.UnauthorizedAccessException - 拒绝访问路径

c# - Json 返回列表中的列表

c# - UI 自动化和功能区控件

c# - 以编程方式检查 IIS7 启动画面

c# - 将 C 结构数组编码为 C#

c# - .NET Windows 服务 - 架构决策

C# - 传递一个可能在其他地方改变的 bool 值,但我不想改变方法

c# - log4net 不能在 Windows 服务中工作

c# - ServiceController 类未导入 C#

java - 服务无法启动 : error 1067: the process terminated unexpectedly