当Windows操作系统关机时,不会执行Windows Service的OnStop方法(转载)
Windows Service OnStop when computer shutdown
问:
I'm writing a Windows Service in C#. I want to take the same action for when the service is stopped by the Service control panel as when the system is shutdown. I want to take the same action for either case.
Do I have to override ServiceBase.OnShutdown(), or is overriding ServiceBase.OnStop() for both cases sufficient?
答:
Override OnShutdown is the correct method. OnStop is not called during shutdown.
Microsoft Windows has added an option called Fast Startup which does not actually shutdown the computer.
As noted in the Fast Startup setting description, Restart isn't affected. This is why the Restart triggers OnShutdown and Shutdown does not.
Turning off Fast Startup will trigger OnShutdown for both Restart and Shutdown.
所以实际上,在Windows操作系统关机时,会调用的是ServiceBase.OnShutdown()方法(前提是设置了ServiceBase类的CanShutdown属性为true),而ServiceBase.OnStop()方法并不会被调用,但是为了安全起见,我建议在OnStop和OnShutdown方法中都实现Windows Service的停止逻辑,此外我们在OnStop和OnShutdown方法中使用一个锁变量和一个标志变量,使得如果一个方法先执行了Windows Service的停止逻辑,另一个方法就不会执行Windows Service的停止逻辑了。
此外需要注意的是Windows操作系统在关机时,只会给ServiceBase.OnShutdown()方法12秒的执行时间,超过这个时间后Windows Service的进程还是会被强制终止,可以参照这篇文章,使用PreShutdown事件将超时时间扩展为3分钟。
另外,经过测试,DasMulli.Win32.ServiceUtils这个.NET Core的Windows Service框架也有缺陷,在Windows操作系统关机时,并不会调用其IWin32Service.Stop()方法,所以存在很大的安全隐患,请慎用。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2018-09-18 SQL Server 中为何拥有db_owner权限的账号删除不掉数据库
2016-09-18 从JAVA看C#中volatile和synchronized关键字的作用