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:

<br />
thread.Interrupt();<br />
thread.Join();<br />

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:

<br />
thread.Abort();<br />
thread.Join();<br />

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.

Tags: None
Categories: None |

0 comments have been posted.

Your email: we will send you a confirmation link to this address to confirm your identity and to prevent robot posting
Get in touch
»...«
Follow updates

Join our social networks and RSS feed to keep up to date with latest news and publications