C# 如何杀死阻塞的线程?

发布于 2024-10-31 02:49:53 字数 939 浏览 4 评论 0原文

我有一个线程:

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)
}

我正在使用 .NET Zip Library< /a> 并调用 ZipFile.CommitUpdate() ,它在大多数情况下都有效,但有时只是挂起。我不知道为什么,我在文档中看不到任何说明为什么会发生这种情况的内容。一个不应超过 5 - 10 秒的小文件有时会停留超过 5 分钟而没有任何进展。进程资源管理器中的 I/O 图表显示该进程没有读取或写入,并且没有 CPU 使用率。基本上,如果发生这种情况,我想杀死 CommitUpdate() 并在放弃之前重试一两次。

知道如何杀死陷入阻塞调用的线程吗?

(或者 - 那些对这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

小嗲 2024-11-07 02:49:53

如果要使用 Thread.Abort() 终止挂起的线程,请确保在线程代码中处理 ThreadAbortException。正常的模式是:

try {
    // do work here
}
catch (ThreadAbortException) {
    // allows your thread to gracefully terminate
    Thread.ResetAbort();
}
catch {
    // regular exception handling
}

如果您不遵循上述模式,那么您的线程最多会不正常地终止。最坏的情况是,您可能会遇到许多其他问题。

If you're going to terminate the hanging thread by using Thread.Abort(), make sure you handle ThreadAbortException in your thread code. The normal pattern is:

try {
    // do work here
}
catch (ThreadAbortException) {
    // allows your thread to gracefully terminate
    Thread.ResetAbort();
}
catch {
    // regular exception handling
}

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.

在你怀里撒娇 2024-11-07 02:49:53

你可以调用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.

幼儿园老大 2024-11-07 02:49:53

不要使用 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.

倚栏听风 2024-11-07 02:49:53

除了使用秒表之外,您还可以这样做:

Thread thread = new Thread(new ThreadStart(delegate()
{
   threadCode(o);
}));
thread.Start();

thread.Join(myTimeout);
if(thread.IsAlive)
{
   thread.Abort();
}

Alternative to using a Stopwatch, you could do this:

Thread thread = new Thread(new ThreadStart(delegate()
{
   threadCode(o);
}));
thread.Start();

thread.Join(myTimeout);
if(thread.IsAlive)
{
   thread.Abort();
}
長街聽風 2024-11-07 02:49:53

使用 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.

与往事干杯 2024-11-07 02:49:53

作为最后的手段,您可以使用 Thread.Abort() 终止线程 - 不要期望任何有序关闭。

As a last resort you could use Thread.Abort() to terminate the thread - don't expect any orderly shutdown.

堇年纸鸢 2024-11-07 02:49:53

我自己没有这样做过,所以我不确定这一点,但是 t.Abort(); 可以解决这个问题吗?

I haven't done this myself, so I not sure about this, but does t.Abort(); do the trick?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文