问题描述
我有一个线程:
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)
}
我使用的并调用 ZipFile.CommitUpdate()
这工作的大部分时间,但有时只是挂起。我不知道为什么,我不能,它指定为什么发生这种情况的文档中看到什么。这应该不超过5小文件 - 10秒钟即可将有时坐在那里更多然后5分钟内无进展。 I /过程探险表演O图形,进程不读或写,并没有CPU使用率。基本上,如果发生这种情况,我想杀死 CommitUpdate()
,并尝试放弃之前再一次或两次。
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?
(或者 - 那些你用这个压缩库的经历:你知道为什么它可能是一些文件有时挂?我修改的.docx和.pptx(GZip压缩)文件的内容。这将是一个理想的解决方案。)
(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.)
推荐答案
您可以致电 Thread.Abort的
来血腥谋杀的主题。
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.
这篇关于C#如何杀死一个被阻塞的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!