在 Thread.Sleep 期间可以引发 ThreadAbortException 吗?
Thread.Abort 可以中断正在睡眠的线程(例如使用 Thread.Sleep(TimeSpan.FromDays(40)) 吗?或者它会等到睡眠时间跨度到期吗?
(备注:FromDays(40) 当然是一个笑话。我知道 Thread.Abort 不是停止线程的推荐方法,我正在使用现在不想重构的遗留代码。)
Can Thread.Abort interrupt a thread that is sleeping (using, say, Thread.Sleep(TimeSpan.FromDays(40)) ? Or will it wait until the sleep time span has expired ?
(Remarks: FromDays(40) is of course a joke. And I know Thread.Abort is not a recommended way to stop a thread, I'm working with legacy code that I don't want to refactor for now.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代码胜千字:
程序在睡眠时间耗尽之前结束。
Code is worth a thousand words:
The program ends before the sleep time is exhausted.
您只能从另一个线程中中止该线程。也就是说,您应该将 Thread 引用存储在某处,然后从睡眠线程以外的线程调用
.Abort
。You can abort the thread from another thread only. That is, you should store the Thread reference somewhere and then call
.Abort
from a thread other than the one which is sleeping.