Link to home
Start Free TrialLog in
Avatar of EwS
EwS

asked on

How to stop a Windows Service gracefully?

I have a Windows Service which processes some files every X seconds. The service should run 24/7 unless there is an exception, in which case I stop the service. However, there are times when I would want to stop the service manually (through Admin Tools/Services). How do I ensure that when I do that while the function that does the file processing is still executing, the service will wait for the function to finish? Is there anything I can put in OnStop() to detect whether the function is running or not? I suppose I could have the function turn some app settings on when it starts executing and off when it finishes executing, and add some code in OnStop() to check the setting. However, I would have to implement a loop to constantly check for the flag, plus I don't know how long OnStop() can wait?

I would appreciate any suggestions. Thank you.
SOLUTION
Avatar of Jason Evans
Jason Evans
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of slado2
slado2

If your service takes some time to stop you should inform servicemanager that the service status is stop pending and you need some more time.
http://msdn2.microsoft.com/en-us/library/d56de412(VS.80).aspx
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial