Windows OnStop Service on Computer Shutdown

I am writing a Windows Service in C #. I want to take the same action as stopping the service in the Service control panel as when shutting down the system. I want to do the same action for both cases.

Do I need to override ServiceBase.OnShutdown()

or override ServiceBase.OnStop()

for both cases?

+5


source to share


2 answers


Yes. OnStop()

called when the machine is turned off. OnShutdown()

is when you need to know the car is shutting down.



+4


source


Override OnShutdown

is the correct method. OnStop

not called during shutdown.

Microsoft Windows has added an option called " Fast Startup

which does not actually turn off the computer.

As noted in the setting description Fast Startup

, Restart

this is not affected. That is why it Restart

calls OnShutdown

a Shutdown

- no.



Disabling Fast Startup

will trigger OnShutdown

for Restart

and Shutdown

.

Power Options System Settings

0


source







All Articles