When the service control manager stops your service, the .Net ServiceBase sets the status to SERVICE_STOP_PENDING and calls your OnStop method. What you do with this notification is up to you. Ideally, you will have some kind of mechanism, for example, using a CancellationToken to notify you of any of your threads that are currently working, so that you need to wrap them up and exit as quickly as possible. In your case, this may be due to the closure of the ServiceHost service. Your OnStop method must wait until it finds out that this has been done (by joining threads, for example) before returning. There are ways to notify the service control manager that you need more time, or simply tell him that you have received a notification and you are actively working on a stop (SERVICE_STATUS dwcheckpoint).
OnStop ( ), , . , . OnStop , . OnStop, , , , , (20 ... ), .
- , , , .. , , , , . , " ".
http://blogs.msdn.com/b/bclteam/archive/2009/02/19/in-depth-how-net-managed-services-interact-with-the-servicecontrolmanager-scm-kim-hamilton.aspx