Here’s the scenario:
I have a Windows Service that’s running. OnStart() it sets up a timer that will call a function (let’s call it ProcessEvent()). The code inside ProcessEvent is a critical section, so only one thread can do the following:
private void ProcessEvent(object sender, ElapsedEventArgs e)
{
lock(lockObj)
{
string[] list = GetList();
Parallel.ForEach(list, item => { ProcessItem(item) });
}
}
ProcessItem can potentially take a long time.
Now when the service is stopped my OnStop() currently just stops and disposes the timer. However I noticed that even after service is stopped there are threads that are still running ProcessItem().
So how can I kill all running threads spawned by this program (mainly the ones spawned by the Parallel.ForEach but also any that are waiting on the lock in ProcessEvent)?
I know that had I created the thread myself I could set isBackground to true and it will all get killed when process dies but I don’t create these threads manually.
Use the CancellationToken structure. Read this and this for more information.