From the MSDN link you provided:
A thread is either a background thread or a foreground thread. Background threads are identical to foreground threads, except that background threads do not prevent the process from ending. As soon as all the foreground threads belonging to the process are complete, the common language runtime terminates the process. Any remaining background threads stop and do not end.
Setting the IsBackground thread IsBackground to true will allow your Windows service to stop working immediately after the OnStop() callback completes, and all the foreground threads (if any) are completed. Background threads will be stopped, wherever they are in the state of their execution, so if these threads should legally terminate, you will need to use a different mechanism.
One way to do this is to use front threads, which check the ManualResetEvent object, which signals that threads are disconnecting. In the OnStop() ManualResetEvent , set ManualResetEvent , and then wait for the threads to finish using Join() . If they do not exit within a reasonable time, you can forcefully terminate them from the moment you exit the process.
Matt davis
source share