More Events

As you have already seen in the previous section there are several events available:
  • OnRegisteredMessage
  • OnServiceStateChanged
  • OnServiceExiting

The first event has already been covered in the previous section.

OnServiceStateChanged

As described, services should support at least the functionality to change their state if they are started, quit, suspended and resumed.

For this purpose the Property ServiceState is existing in the ServiceApplication class.
ServiceState.png

This state shows the current state of your service.

There are 2 possibilities, how this Property gets changed:
  1. Through an received Windows Message
  2. By setting the Property manually inside your code.

Let's assume, your Service is actually is being started. The first state in this case is ServiceState.Starting, which is equal to a Initialization phase.
If the Service is ready to retrieve Windows Messages, the state is changed to ServiceState.Running.

This means, that each state transition has at least 2 changes in ServiceState:
  • ServiceApplication.Start changes the current state to ServiceState.Starting, followed by ServiceState.Running
  • ServiceApplication.Quit changes the current state to ServiceState.Exiting, followed by ServiceState.Exit or ServiceState.Running
  • ServiceApplication.Suspend changes the current state to ServiceState.Suspending, followed by ServiceState.Suspend
  • ServiceApplication.Resume changes the current state to ServiceState.Resuming, followed by ServiceState.Running

As you can see, each state consists of something like a before and after message being fired.

If you want your application to be informed about any change in the ServiceState, you can create an EventHandler for this purpose.

Let's take a look at the following sample:
...
private ManagedServiceSample1()
{
    ...
    this.OnServiceStateChanged += new ServiceStateChangedEventHandler(ManagedServiceSample1_OnServiceStateChanged);
}

void ManagedServiceSample1_OnServiceStateChanged(ServiceStateEventArgs serviceStateEventArgs)
{
    switch (serviceStateEventArgs.ServiceState)
    {
        case ServiceState.Exited:
            break;
        case ServiceState.Exiting:
            break;
        case ServiceState.Resuming:
            break;
        case ServiceState.Running:
            break;
        case ServiceState.Starting:
            break;
        case ServiceState.Suspended:
            break;
        case ServiceState.Suspending:
            break;
        default:
            break;
    }
}


As you can see adding an eventhandler is pretty easy. Your eventhandler receives an object of type ServiceStateEventArgs, which contains the readonly property ServiceState. This contains the newly set ServiceState information.

If now a Windows Message is received for suspending a service 2 events get fired as described above.
By catching these messages you are enabled to execute custom code, if it is necessary for your Service application.

If you change the ServiceState manually in your code, the event get's fired as well.

OnServiceExiting

As you have seen, ServiceApplication.Quit() can result in SterviceState.Exited or in ServiceState.Running.

The reason for this lies in the event OnServiceExiting:
...
private ManagedServiceSample1()
{
    ...
    this.OnServiceExiting += new ServiceExitingEventHandler(ManagedServiceSample1_OnServiceExiting);
}

void ManagedServiceSample1_OnServiceExiting(System.ComponentModel.CancelEventArgs cancelEventArgs)
{
    cancelEventArgs.Cancel = true;
}


If you create an eventhandler for this event, a parameter of type CancelEventArgs is being received.
Besides of the fact, that you can run clean up code in this method, you can cancel the exiting service as well. This functionality is similar to the Closing Event of a Windows Form, which can be canceled as well.

If the Property Cancel of cancelEventArgs is set to true, Quitting of the Service is stopped and the Service state changes back to ServiceState.Running.

The purpose for this can be, if you create a Service, which shouldn't be allowed to be quited.

Last edited May 31, 2008 at 12:06 PM by PeterNowak, version 3

Comments

No comments yet.