If killing jobs is crucial to the workflow, put each "killable" unit of work in a separate process, which you can then kill much more easily using Process.Kill()
.
Why? Because threads are relatively hard to manage, especially stop/kill them, especially when you don't control the code that runs on them.
Why is that? Because the ways to kill threads are:
- Cooperatively: Signal the thread to stop, then wait for the thread's code to see the signal and exit. That's how Task Canellation works. CON: If the code doesn't check for cancellation during long/block operations, the cancel will not happen. And if you don't control the code, you can't add that.
- Pre-emptively: Using
Thread.Abort()
or its unmanaged equivalent. There are many issues with it, see What's wrong with using Thread.Abort()