C# 如何杀死阻塞的线程?
我有一个线程:
void threadCode(object o)
{
doStuffHere(o); // Blocking call. Sometimes hangs.
}
我这样称呼它:
Thread t = new Thread(new ThreadStart(delegate()
{
threadCode(o);
}));
t.Start();
StopWatch sw = new StopWatch();
sw.Start();
while (t.IsAlive)
{
Application.DoEvents();
if (sw.EllapsedMilliseconds > myTimeout)
// Somehow kill t even though doStuffHere(o) is blocked (but hung)
}
知道如何杀死陷入阻塞调用的线程吗?
(或者 - 那些对这个 zip 库有经验的人:你知道为什么它有时会与某些文件一起挂起吗?我正在修改 .docx 和 .pptx (GZip) 文件的内容。这将是一个理想的解决方案。 )
I have a thread:
void threadCode(object o)
{
doStuffHere(o); // Blocking call. Sometimes hangs.
}
and I'm calling it like this:
Thread t = new Thread(new ThreadStart(delegate()
{
threadCode(o);
}));
t.Start();
StopWatch sw = new StopWatch();
sw.Start();
while (t.IsAlive)
{
Application.DoEvents();
if (sw.EllapsedMilliseconds > myTimeout)
// Somehow kill t even though doStuffHere(o) is blocked (but hung)
}
I'm using the .NET Zip Library and calling ZipFile.CommitUpdate()
which works most of the time, but sometimes just hangs. I don't know why, I can't see anything in the documentation which specifies why this is happening. A small file which should take no longer than 5 - 10 seconds will sometimes sit there for more then 5 minutes with no progress. I/O graphs in process explorer show that the process is not reading or writing, and there is no CPU usage. Basically, if this happens, I want to kill CommitUpdate()
and try again once or twice before giving up.
Any idea how I can kill a thread stuck in a blocking call?
(Alternatively - those of you with experience with this zip library: do you know why it might be hanging with some files sometimes? I'm modifying the contents of .docx and .pptx (GZip) files. This would be an ideal solution.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果要使用 Thread.Abort() 终止挂起的线程,请确保在线程代码中处理 ThreadAbortException。正常的模式是:
如果您不遵循上述模式,那么您的线程最多会不正常地终止。最坏的情况是,您可能会遇到许多其他问题。
If you're going to terminate the hanging thread by using
Thread.Abort()
, make sure you handleThreadAbortException
in your thread code. The normal pattern is:If you don't follow the above pattern, then at best your threads will terminate ungracefully. At worst, you could run into a number of other problems.
你可以调用
Thread.Abort
来血腥地杀死线程。线程将无法在自身之后进行清理(
finally
块除外);取决于它当时正在做什么,它可能会严重扰乱你的程序。You can call
Thread.Abort
to bloodily murder the thread.The thread will not be able to clean up after itself (except for
finally
blocks); depending on exactly what it was doing at the time, it could seriously mess up your program.不要使用 Thread.Abort() 来解决您正在使用的库中的错误。 (或者它可能是您的代码中的错误)。
要么修复原来的错误,要么移动到不同的库。
ps:您可能想尝试 DotNetZip,而不是 SharpZipLib。
DO NOT use Thread.Abort() to workaround a bug in a library you're using. (Or it could be a bug in your code).
Either get the original bug fixed, or move to a different library.
ps: You might want to try DotNetZip, in lieu of SharpZipLib.
除了使用秒表之外,您还可以这样做:
Alternative to using a Stopwatch, you could do this:
使用 Thread.Abort() 方法终止线程。请参阅 http://msdn.microsoft.com/en-us /library/aa332365(VS.71).aspx 了解更多详细信息。
Use the Thread.Abort() method to kill a thread. See http://msdn.microsoft.com/en-us/library/aa332365(VS.71).aspx for more details.
作为最后的手段,您可以使用
Thread.Abort()
终止线程 - 不要期望任何有序关闭。As a last resort you could use
Thread.Abort()
to terminate the thread - don't expect any orderly shutdown.我自己没有这样做过,所以我不确定这一点,但是 t.Abort(); 可以解决这个问题吗?
I haven't done this myself, so I not sure about this, but does
t.Abort();
do the trick?