Windows Service OnStop when computer shutdown
Yes. OnStop()
gets called when the machine is shutdown. OnShutdown()
is for when you need to know specifically that the machine is being shutdown.
UPDATE:
As has been pointed out in the comments since this was first posted, this is no longer necessarily the case. So your code should be written with the assumption that OnStop()
may or may not be called when the machine is shut down. If you need to clean up during a shutdown, handle OnShutdown()
.
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
.