c#中的threadinterruptedexception是线程被中断时抛出的异常,表示有其他线程调用了interrupt()方法,用于实现合作式线程取消;1. 它不是错误,而是一种中断信号,表明线程应停止当前操作并退出;2. 处理方式是在try-catch中捕获该异常,进行资源清理后优雅退出;3. 与thread.abort()不同,interrupt()是协作式的,不会强制终止线程,避免数据损坏和资源泄露;4. 响应中断时应立即清理资源、退出循环或方法,并考虑是否需要重新设置中断状态以传递信号;5. 现代c#推荐使用cancellationtoken替代thread.interrupt(),因其支持非阻塞检查、多任务取消及与tpl和async/await的深度集成,提供更灵活和安全的取消机制。

C#中的
ThreadInterruptedException
Thread.Sleep
Thread.Join
Monitor.Wait
Interrupt()
处理
ThreadInterruptedException
最直接的办法是在可能抛出此异常的代码块外部使用
try-catch
using System;
using System.Threading;
public class ThreadInterruptExample
{
public static void Main(string[] args)
{
Thread workerThread = new Thread(DoWork);
workerThread.Start();
// 让主线程稍等片刻,给工作线程一些时间
Thread.Sleep(1000);
Console.WriteLine("主线程:准备中断工作线程...");
workerThread.Interrupt(); // 中断工作线程
workerThread.Join(); // 等待工作线程结束
Console.WriteLine("主线程:工作线程已结束。");
}
static void DoWork()
{
Console.WriteLine("工作线程:开始工作...");
try
{
// 模拟一个长时间的阻塞操作
Console.WriteLine("工作线程:将休眠5秒...");
Thread.Sleep(5000);
Console.WriteLine("工作线程:休眠结束,继续工作。");
}
catch (ThreadInterruptedException ex)
{
Console.WriteLine($"工作线程:捕获到中断异常 - {ex.Message}");
// 这里可以进行一些清理工作
Console.WriteLine("工作线程:执行清理并准备退出。");
// 捕获后,通常会退出循环或方法
return;
}
catch (Exception ex)
{
// 捕获其他可能的异常
Console.WriteLine($"工作线程:捕获到其他异常 - {ex.GetType().Name}: {ex.Message}");
}
finally
{
// 无论如何,确保资源被释放
Console.WriteLine("工作线程:清理完成,线程即将结束。");
}
}
}这段代码展示了一个典型的处理流程。当
Thread.Sleep
ThreadInterruptedException
catch
线程中断,或者说
Thread.Interrupt()
Interrupt()
这与已经被废弃的
Thread.Abort()
Thread.Abort()
ThreadAbortException
说白了,
Thread.Abort()
Thread.Interrupt()
Thread.Abort()
优雅地响应
ThreadInterruptedException
finally
catch
ThreadInterruptedException
Thread.CurrentThread.Interrupt()
Join
Sleep
using System;
using System.Threading;
public class GracefulInterruption
{
public static void Main(string[] args)
{
Thread worker = new Thread(LongRunningTask);
worker.Start();
Thread.Sleep(2000); // 运行2秒
Console.WriteLine("主线程:发送中断信号。");
worker.Interrupt();
worker.Join();
Console.WriteLine("主线程:工作线程已完成或被中断。");
}
static void LongRunningTask()
{
Console.WriteLine("工作线程:任务开始。");
try
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine($"工作线程:正在处理第 {i + 1} 步...");
// 模拟一个可能被中断的阻塞操作
Thread.Sleep(1000);
}
Console.WriteLine("工作线程:任务自然完成。");
}
catch (ThreadInterruptedException)
{
Console.WriteLine("工作线程:捕获到中断,正在清理...");
// 这里是清理资源的好地方
Console.WriteLine("工作线程:资源已清理。");
// 考虑是否需要重新设置中断状态,取决于更上层的逻辑
// Thread.CurrentThread.Interrupt();
return; // 退出任务
}
catch (Exception ex)
{
Console.WriteLine($"工作线程:发生意外错误 - {ex.Message}");
}
finally
{
Console.WriteLine("工作线程:无论如何,任务结束。");
}
}
}在现代C#编程中,特别是涉及到异步编程(
async/await
Thread.Interrupt()
CancellationTokenSource
CancellationToken
CancellationTokenSource
CancellationToken
CancellationTokenSource
CancellationTokenSource.Token
token.IsCancellationRequested
OperationCanceledException
token.ThrowIfCancellationRequested()
CancellationTokenSource.Cancel()
优点:
IsCancellationRequested
CancellationTokenSource
Task.Run
Parallel.For
PLINQ
CancellationToken
OperationCanceledException
Thread.Interrupt()
Thread
CancellationToken
CancellationToken
ThreadInterruptedException
Thread.Sleep
以上就是C#的ThreadInterruptedException是什么?线程中断处理的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号