How to terminate a thread in C#?

Thread.Abort will “kill” the thread, but this is roughly equivalent to:

Scenario: You want to turn off your computer

Solution: You strap dynamite to your computer, light it, and run.

It’s FAR better to trigger an “exit condition”, either via CancellationTokenSource.Cancel, setting some (safely accessed) “is running” bool, etc., and calling Thread.Join. This is more like:

Scenario: You want to turn off your computer

Solution: You click start, shut down, and wait until the computer powers down.

Leave a Comment