当Windows操做系统关机时,不会执行Windows Service的OnStop方法(转载)

Windows Service OnStop when computer shutdownhtml

 

 

问:windows


 

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?框架

 

 

答:ide


 

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.atom

As noted in the Fast Startup setting description, Restart isn't affected. This is why the Restart triggers OnShutdown and Shutdown does not.spa

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的中止逻辑了。htm

 

此外须要注意的是Windows操做系统在关机时,只会给ServiceBase.OnShutdown()方法12秒的执行时间,超过这个时间后Windows Service的进程仍是会被强制终止,能够参照这篇文章,使用PreShutdown事件将超时时间扩展为3分钟。

 

另外,通过测试,DasMulli.Win32.ServiceUtils这个.NET Core的Windows Service框架也有缺陷,在Windows操做系统关机时,并不会调用其IWin32Service.Stop()方法,因此存在很大的安全隐患,请慎用。

 

 

原文连接

相关文章
相关标签/搜索