I have a VB.NET Windows Service that I wrote and it works fine. In VB.NET there is an overrideable method called OnStop() that gets called when the service is stopped. However, it appears it only gets stopped if someone actually stops the service from the Services MMC Console Window.
My question is, is there an event or an overridable method that gets called whenever the service stops, regardless of how it got stopped (e.g. Computer reboots, Exception occured and shut down service, etc….)?
There is no one event to handle all of those events. Most of the events you listed can be dealt with on an individual basis.
Note that you cannot depend on these events firing 100% of the time. There are situations where each event will fail to run. For instance, the power related events won’t fire if I walk up and rip out the power cord. Your code must account for the service shutting down without any prior notification.