I am writing a Service in 'C' which has lots of database activity. When the user requests to STOP the Service (within the Service Control Manager) the program simply halts. It does not matter what it is in the middle of doing.

How do I recognize that a STOP has been requested? I need this so I can shutdown the service gracefully.

There is a nice example of a Service posted on this web-site and it shows how to pause or stop a service but both of these happen instantly without any warning.

Any help is greatly appreciated!