Classes

Class ServiceApp() Foundation

Class function of the ServiceApp class

Description

The ServiceApp class is used to implement Service applications on Windows platforms capable of running services. For technical background information please refer to the section Implementing services.

The class is unique in the way that it consists only of the class object and no instances can be created. This reflects the situation that only one service can be implemented in a single executable file. Therefore, only one object is required per service application and there are only class methods and class variables belonging to the class object. As a result, the ServiceApp class does not have a :new() or :init() method.

There are two ways of implementing a service: using callback code blocks or callback methods. This concept is similar to the callback implementation of Xbase Parts where the reaction to a particular event is programmed by assigning a code block to an instance variable or by overloading the corresponding method in a derived class. In the ServiceApp class, the callbacks are used to implement the reaction to a particular Control request.

A minimum of two callbacks must be implemented that contain the code for the task of the service and how to stop it after being started. The callbacks are :main() and :stop(), both of which are member variables or methods of the class object.

no limitation exists for spawning multiple threads in a service application when concurrent tasks are to be fulfilled.

The class ServiceApp() must not be mixed up with XppApplication() or XbpApplication().

Starting a service
:start()
This class method starts the service process.
Callbacks for Control requests

These class methods are called from the system as a response to control requests from a service control application. It is the programmer's responsibility to implement the behaviour in response of these messages.

continue()
This callback is executed when the service receives the Continue control request.
main()
This callback is called when the service is started.
pause()
This callback is executed when the service receives the Pause control request.
stop()
This callback is executed when the service receives the Stop control request.
Examples
Application implementing a service.
// This sample demonstrates how a service can be implemented. 
// The service simply calls the function Tone() every 
// five seconds until it receives the Stop request. 
// To install and start this service see the 
// example for the ServiceController class. 

#include "service.ch" 

// Entry point of the executable 
PROCEDURE Main() 
   MyService():start() 
RETURN 


CLASS MyService From ServiceApp 
   EXPORTED: 
   CLASS METHOD main 
   CLASS METHOD stop 

   HIDDEN: 
   CLASS VAR lRunning 
ENDCLASS 


// Entry point of the service 
CLASS METHOD MyService:main() 
   ::lRunning := .T. 

   DO WHILE ::lRunning 
      Tone( 400, 9 ) 
      Sleep( 500 ) 
   ENDDO 
RETURN self 


// Entry point for Stop request 
CLASS METHOD MyService:stop() 
   ::lRunning := .F. 
RETURN self 
Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.