Do I really need onShutdown()

G

Guest

For a C# windows service, it seems like the best practice is to only
implement OnStop().
Is that correct? Do I only need OnShutdown if I need to take different
actions when the server is shutdown?
When the server is shutdown and it first tries to stop all the processes,
does this cause OnStop to be invoked assuming the shutdown executes all it's
steps successfully?
 
D

Daniel O'Connell [C# MVP]

knf said:
For a C# windows service, it seems like the best practice is to only
implement OnStop().
Is that correct? Do I only need OnShutdown if I need to take different
actions when the server is shutdown?

Yes, OnStop is all you need unless you need to detect the computer shutting
down.
When the server is shutdown and it first tries to stop all the processes,
does this cause OnStop to be invoked assuming the shutdown executes all
it's
steps successfully?
It should, test it.
 
G

Guest

Could you elaborate that ?
Is the SCM that sends the 'stop' command to the services when the system is
shutting down or is it implemented in .NET?

Thanks
 
D

Daniel O'Connell [C# MVP]

Jimmy Liu said:
Could you elaborate that ?
Is the SCM that sends the 'stop' command to the services when the system
is
shutting down or is it implemented in .NET?

That should be the SCMs responsibility.
 
G

Guest

Thanks for the answer. I actually couldn't find any Microsoft's documentation
says that the SCM sends 'stop' command to Windows services when the system is
shutting down. Could you kindly post a link or book title?
 
S

Stephen Martin

In general you should implement both OnStop and OnShutdown. OnStop is called
when the SCM receives a request to stop the service it is not called when
the system is shutdown. OnShutdown is called when the system is shutting
down and you have registered for the shutdown call.

In theory, you need to set the service's CanShutdown property to true to
register for the OnShutdown call but due to a bug in the ServiceBase class
if your CanStop property is true, as it is by default, then you are
automatically registered for OnShutdown even if you set CanShutdown to
false.
 
D

Daniel O'Connell [C# MVP]

Jimmy Liu said:
Thanks for the answer. I actually couldn't find any Microsoft's
documentation
says that the SCM sends 'stop' command to Windows services when the system
is
shutting down. Could you kindly post a link or book title?

I don't have any, I'm basing this entirely on personal experiance(what I
remember anyway). That is why I recommended testing it yourself, since I
could find no explicit documentation either. Note I said "should" ;)
 
D

Daniel O'Connell [C# MVP]

Stephen Martin said:
In general you should implement both OnStop and OnShutdown. OnStop is
called when the SCM receives a request to stop the service it is not
called when the system is shutdown. OnShutdown is called when the system
is shutting down and you have registered for the shutdown call.

I seem to recall it being called in the distant past, but you may well be
right, although logically stop *should* be called whether the system is
shutting down or not, since the service is stopping.
In theory, you need to set the service's CanShutdown property to true to
register for the OnShutdown call but due to a bug in the ServiceBase class
if your CanStop property is true, as it is by default, then you are
automatically registered for OnShutdown even if you set CanShutdown to
false.

I wouldn't rely on that. Who knows if this behaviour will be emulated in
other frameworks or if MS will fix it sometime in the future.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top