Shutting down a windows service that has threads
Posted
by Dave
on Stack Overflow
See other posts from Stack Overflow
or by Dave
Published on 2010-05-08T17:07:42Z
Indexed on
2010/05/08
17:48 UTC
Read the original article
Hit count: 297
I have a windows service written in .NET 3.5 (c#) with a System.Threading.Timer that spawns several Threads in each callback. These are just normal threads (no Thread Pool) and I've set the IsBackground = true on each thread since I'm only going to be running managed code.
When a user stops the service, what happens to all the threads? Do they die gracefully? I don't have any code that manages the threads via calling join or abort. Is it correct to assume the IsBackground = true is enough to assume the threads will be disposed and stopped when a user stops the service? What exactly happens when someone stops a windows service via the Service Manager GUI? Does it kill the process after it fires the OnStop event?
This would actually be acceptable for me because I've built a separate mechanism that allows a user know for sure there are no threads before they stop the service. This is done via 2 WCF methods exposed from a ServiceHost that runs inside the Windows Service. There's one method to stop spawning new threads and another method to query how many running threads there are left.
I'm just curious what happens if they skip those steps and just stop the service... It seems the IsBackground helps achieve this:
© Stack Overflow or respective owner