.NET threads Abort vs Interrrupt

Not an uncommon task in multithreaded programming - what if you want to terminate a background thread, which is blocked at the moment, and then to make sure it has actually exited. In .NET, you can do the following:


Unfortunately, the call to Join can happen before the thread interruption instruction is issued to the other thread. That means that the thread will be in Join state at the time of processing that instruction. And, according to Microsoft's documentation, that will prevent it from being interrupted!

Abort is your friend in this scenario. This code will work much better:


Of course, if you can terminate your threads by less drastic means, by sending signals etc, it will be better, but sometimes that is the only way.

Add new comment