C# .NET 中停止一个线程

在下载一些文件的时候,网络 IO 可能导致超时,于是想要杀掉那个线程,但是却发现这个不太好做,查了一下,这里说,http://stackoverflow.com/quest…

The reason it’s hard to just kill a thread is because the language designers want to avoid the following problem: your thread takes a lock, and then you kill it before it can release it. Now anyone who needs that lock will get stuck.

What you have to do is use some global variable to tell the thread to stop. You have to manually, in your thread code, check that global variable and return if you see it indicates you should stop.

原来是这样。。

Leave a Reply

Your email address will not be published. Required fields are marked *