If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

How to recognize a STOP in Windows Services

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.

you call RegisterServiceCtrlHandler(...) (in your ServiceMain(...)and pass it your Handler function, you process the stop inside of your handler function.
Can you post the link to the article in question so I can look at what you are referencing.

Re: How to recognize a STOP in Windows Services

Originally posted by mattl
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?

STOP request for service looks like getting SERVICE_CONTROL_STOP control message via its control handler routine.

I need this so I can shutdown the service gracefully.

Service been requested to sop itself has to set its state to SERVICE_STOP_PENDING, to initiate a cleanup procedure and after cleanup ends it has to set its state to SERVICE_STOP notifying the system it's done. It has about 30 seconds (since SERVICE_CONTROL_STOP been emitted) to do it before system will decide to kill it as hanging process. If the service needs more than 30 seconds it has to use checkpoint mechanism.

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.

There is a really nice book on this questions:
Jeffrey Richter, Programming Server-Side Applications for MS Windows 2000. Spend a bit of your money and your time on it - and you'll never regret for money and time you've spent (if you gonna dive into windows service programming more than once).