The proper way to handle this is to have the flag object that you are signaling.
Code running on these threads should periodically check this flag to see if it should exit.
For example, a ManualResetEvent object is suitable for this.
Then you can ask the threads to exit as follows:
evt.Set();
and inside the threads you check for it like this:
if (evt.WaitOne(0)) return;
Secondly, since you use the thread pool, it happens that all the elements that you queued will be processed anyway, but if you add the if statement above to the very beginning of the thread method, it will exit immediately. If this is not enough, you should create your own system using regular threads, so you have full control.
Oh, and just to make sure you don't use Thread.Abort. Ask the threads to exit normally, do not kill them directly.
source share