Contents | Prev | Next

3.9.3.2 Writing well behaved services

Irie Pascal makes it very easy to create services, see creating services for more information. However there is one thing you will need to take care. It is up to you to make sure that the services you create stop promptly when requested to do so. Services are requested to stop when Windows is shutting down, and when you stop them using the Services Control Applet in the Control Panel. In either case the system will not wait forever for your service to stop. Well behaved services should not take too long (certainly not longer than a few seconds) to stop. Irie Pascal supports two built-in functions that you can use to detect when your service has been request to stop. These two functions are the StopServiceEvent function and the wait function.

The StopServiceEvent Function

The StopServiceEvent function takes no arguments and returns one of two values depending on whether your application is a service or not. If your application is not a service then the StopServiceEvent function will always return zero. If you application is a service then the StopServiceEvent function will return a handle to the event object that will signal when your service has been requested to stop. See the Irie Pascal Programmer's Reference Manual (in "progref.html") for more information.

The Wait Function

The wait function takes at least two arguments, the first argument is the maximum length of time the function should wait (in milliseconds). The other arguments are handles to objects that the wait function should wait on. The wait function will wait until one of the objects it is waiting on signals it or until the maximum wait interval expires. The wait function returns the handle to the first object that signals it, or zero if no object signals it before the maximum wait interval expires. NOTE: If the value of the first argument to the wait function is zero then the wait function doesn't actually wait instead it just checks whether any of the objects it is waiting on has signaled it. If the value of the first argument to the wait function is equal to the built-in contant maxword then the wait function will wait forever for one the objects it is waiting on to signal it. See the Irie Pascal Programmer's Reference Manual (in "progref.html") for more information.

Well Behaved Services

Well behaved services will frequently check to see if they have been requested to stop by calling the wait function and passing the handle returned by the StopServiceEvent function as the second argument. The first argument will usually be zero so that no time is wasted if the services has not been requested to stop.

The authoritative source of information about services is the Microsoft Developers Network (MSDN). You can access the MSDN on the Microsoft website at msdn.microsoft.com.

Contents | Prev | Next