C# 线程终止和 Thread.Abort() [英] C# Thread Termination and Thread.Abort()

查看:56
本文介绍了C# 线程终止和 Thread.Abort()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 MSDN 中,Thread.Abort() 方法的描述说:调用此方法通常会终止线程."

In MSDN, the description of the Thread.Abort() method says: "Calling this method usually terminates the thread."

为什么不总是?

在哪些情况下它不会终止线程?

In which cases it doesn't terminate the thread?

还有其他可能终止线程吗?

Are there any other possibility to terminate threads?

推荐答案

Thread.Abort() 在线程上注入一个 ThreadAbortException.线程可以通过调用Thread.ResetAbort() 来取消请求.此外,还有某些代码部分,例如 finally 块将在处理异常之前执行.如果由于某种原因线程卡在这样的块中,线程将永远不会引发异常.

Thread.Abort() injects a ThreadAbortException on the thread. The thread may cancel the request by calling Thread.ResetAbort(). Also, there are certain code parts, such as finally block that will execute before the exception is handled. If for some reason the thread is stuck in such a block the exception will never be raised on the thread.

由于调用者在调用 Abort() 时对线程状态的控制很少,因此通常不建议这样做.将消息传递给请求终止的线程.

As the caller has very little control over the state of the thread when calling Abort(), it is generally not advisable to do so. Pass a message to the thread requesting termination instead.

这篇关于C# 线程终止和 Thread.Abort()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆