The base class for all the windows services is ServiceBase
class. ServiceBase class has methods like OnStart, OnStop, OnPause and
OnContinue which can be overridden by the derived classes. Let us see the
ServiceBase class in detail and understand it before we create a windows
service. When the service starts the OnStart method of the service runs. The
Service Control manager is responsible for running the Service. All the events,
like OnStart, OnStop, etc. are accessed by the Service control Manager. It is
to be understood that this is not similar to normal executable file execution.
The executable initially calls the derived class constructor of ServiceBase.
After this the OnStart method takes on. It is to be understood that all the
resources or objects that are needed to be cleared off in the Onstop event
should be created in Onstart event and not the constructor.
The following are the methods or events of ServiceBase
class. All these events can be overridden.
1.
OnStart – This method is fired when the service is started by the
Service Control Manager. This method is used to specify the processing that
occurs when a Start command is received.
2.
OnStop – This method is called when the service is stopped by the
Service Control Manager. This is typically used to specify the processing that
occurs when a Stop command is received by the Service Control Manager.
3.
OnPause – This method is called when the service is paused and typically
contains the processing to pause the service.
4.
OnContinue – This method is called when the service is resumed after
being paused. This method typically contains the necessary processing so
as to enable a service to return to normal functioning after the same was
paused earlier.
5.
OnShutdown – This method is called when the system is being shutdown and
contains the necessary processing indicative of what should happen prior to the
system shutting down.
6.
OnPowerEvent – This method is used to specify the necessary processing
that should take place when the power status changes.
7.
OnCustomCommand – This method is used to specify a custom command, i.e.,
any command other than those discussed above.