I have created a Windows Service in VB.NET (VS2010) that executes a certain task every minute. When the service is being stopped, either manually by the user, or when the system is rebooted, what can I do to make sure the task is being finished properly before the service is actually terminated?
Share
You should override the Service’s OnStop event.
Here you should finish your task and release all resources that need to be released.
The constructor of the service will not be called again when it’s restarted. Because of this, you should not use the constructor to perform startup processing, rather use the
OnStart()method to handle all initialization of your service, andOnStop()to release any resources.Reduced to your needs, it might be sufficient to stop the timer so that it could simply be restarted in
OnStart:If your task is started from it’s own thread, you can ensure that it will be finished properly.
When it’s instead running in the main-thread and…
Read more: http://www.codeproject.com/KB/system/LexnnWinService.aspx