473,320 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Do I really need onShutdown()

knf
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?
Nov 17 '05 #1
7 8566

"knf" <kn*@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com...
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.
Nov 17 '05 #2
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

"Daniel O'Connell [C# MVP]" wrote:

"knf" <kn*@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com...
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.

Nov 17 '05 #3

"Jimmy Liu" <Jimmy Li*@discussions.microsoft.com> wrote in message
news:C1**********************************@microsof t.com...
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.
Nov 17 '05 #4
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?

"Daniel O'Connell [C# MVP]" wrote:

"Jimmy Liu" <Jimmy Li*@discussions.microsoft.com> wrote in message
news:C1**********************************@microsof t.com...
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.

Nov 17 '05 #5
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.
"knf" <kn*@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com...
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?

Nov 17 '05 #6

"Jimmy Liu" <Ji******@discussions.microsoft.com> wrote in message
news:2C**********************************@microsof t.com...
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" ;)
Nov 17 '05 #7

"Stephen Martin" <sm*****@removethis.emsoft.andthis.ca> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Jeannie | last post by:
Hello group! I'm in Europe, traveling with my laptop, and I don't any compilers other than Borland C++ 5.5. available. I also don't have any manuals or help files available. Sadly, more...
0
by: TDC | last post by:
At system start I get the OnStart. When the system shuts down, do I *just* get an OnShutdown, or will I get both and OnStop and an OnShutdown? If you only get the OnShutdown, I find it odd that...
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
2
by: Peter Meinl | last post by:
Restarting Windows seems not to fire the OnShutdown event in the Windows Service. Does not work on Windows XP SP2 and not on Windows 2003 Server SP1. OnStart and OnStop do work fine. Steps...
1
by: Croco Stimpy | last post by:
I have a service that must know when the OS shuts down, so it can perform some cleanup. I just found that the OnShutDown event never fires when the machine shuts down! I did some checking and...
2
by: lb.weissman | last post by:
Hi, I have a simple service, which I want to stop normally. I added the method OnShutdown, and set the CanShutdown property to true. Still, when I restart the computer my OnShutdown method is...
0
by: Klaus Unterberg | last post by:
Hello, does anyone know, how to make an EventLog in service event OnShutdown? It seems that OnShutdown cannot make an entry to the eventlog.writeentry(). Even when Eventlog is listed in the...
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
14
by: madankarmukta | last post by:
Hi , I want to commit the chages/ want to do some activity on service's OnShutuDown().But i found that It neven get's called ..when i shut down the system or when i power it off..in Both the cases...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.