thread-abort-migration
它能做什么
这项技能把使用 Thread.Abort 的 .NET Framework 代码迁移为现代 .NET 中使用的取消方式。适合移植旧应用、其中用于停止线程的代码在升级后已无法工作的开发者。
适用场景
- ✓当迁移到 .NET 6 或更高版本的项目调用了 Thread.Abort,而它现在会失败时。
- ✓当代码捕获 ThreadAbortException,或使用 Thread.ResetAbort 进行流程控制时。
- ✓当使用 Response.End 或 Response.Redirect 的旧 ASP.NET 代码需要在现代 .NET 上运行的时候。
技能链接
https://github.com/dotnet/skills/blob/main/plugins/dotnet-upgrade/skills/thread-abort-migration/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“升级到 .NET 8 后,我的代码在 Thread.Abort 处失败。应如何用取消机制替换它?”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会找出代码如何使用 Thread.Abort,识别使用模式,并应用匹配的替换方式,例如协作式取消。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Thread.Abort Migration
This skill helps an agent migrate .NET Framework code that uses Thread.Abort to the cooperative cancellation model required by modern .NET (6+). Thread.Abort throws PlatformNotSupportedException in modern .NET — there is no way to forcibly terminate a managed thread. The skill identifies the usage pattern first, then applies the correct replacement strategy.
When to Use
- Migrating a .NET Framework project to .NET 6+ that calls
Thread.Abort - Replacing
ThreadAbortExceptioncatch blocks that use control flow or cleanup logic - Removing
Thread.ResetAbortcalls that cancel pending aborts - Replacing
Thread.Interruptfor waking blocked threads - Migrating ASP.NET code that uses
Response.EndorResponse.Redirect(url, true), which internally callThread.Abort - Resolving
PlatformNotSupportedExceptionorSYSLIB0006warnings after a target framework change
When Not to Use
- The code only uses
Thread.Join,Thread.Sleep, orThread.Startwithout any abort, interrupt, orThreadAbortExceptioncatch blocks. These APIs work identically in modern .NET — no migration is needed. Stop here and tell the user no migration is required. If you suggest modernization (e.g.,Task.Run,Parallel.ForEach), you must explicitly state these are optional improvements unrelated to Thread.Abort migration, and the existing code will compile and run correctly as-is on the target framework. - The project will remain on .NET Framework indefinitely
- The Thread.Abort usage is inside a third-party library you do not control
Inputs
| Input | Required | Description |
|---|---|---|
| Source project or solution | Yes | The .NET Framework project containing Thread.Abort usage |
| Target framework | Yes | The modern .NET version to target (e.g., net8.0) |
| Thread.Abort usage locations | Recommended | Files or classes that reference Thread.Abort, ThreadAbortException, Thread.ResetAbort, or Thread.Interrupt |
Workflow
Commit strategy: Commit after each pattern replacement so the migration is reviewable and bisectable. Group related call sites (e.g., all cancellable work loops) into one commit.
Step 1: Inventory all thread termination usage
Search the codebase for all thread-termination-related APIs:
Thread.Abortandthread.Abort()(instance calls)ThreadAbortExceptionin catch blocksThread.ResetAbortThread.InterruptResponse.End()(calls Thread.Abort internally in ASP.NET Framework)Response.Redirect(url, true)(thetrueparameter triggers Thread.Abort)SYSLIB0006pragma suppressions
Record each usage location and classify the intent behind the abort.
Step 2: Classify each usage pattern
Categorize every usage into one of the following patterns:
| Pattern | Description | Modern replacement |
|---|---|---|
| Cancellable work loop | Thread running a loop that should stop on demand | CancellationToken checked in the loop |
| Timeout enforcement | Aborting a thread that exceeds a time limit | CancellationTokenSource.CancelAfter or Task.WhenAny with a delay |
| Blocking call interruption | Thread blocked on Sleep, WaitOne, or Join that needs to wake up |
WaitHandle.WaitAny with CancellationToken.WaitHandle, or async alternatives |
| ASP.NET request termination | Response.End or Response.Redirect(url, true) |
Return from the action method; use HttpContext.RequestAborted |
| ThreadAbortException as control flow | Catch blocks that inspect ThreadAbortException to decide cleanup actions |
Catch OperationCanceledException instead, with explicit cleanup |
| Thread.ResetAbort to continue execution | Catching the abort and calling ResetAbort to keep the thread alive |
Check CancellationToken.IsCancellationRequested and decide whether to continue |
| Uncooperative code termination | Killing a thread running code that cannot be modified to check for cancellation | Move the work to a separate process and use Process.Kill |
Critical: The fundamental paradigm shift is from preemptive cancellation (the runtime forcibly injects an exception) to cooperative cancellation (the code must voluntarily check for and respond to cancellation requests). Every call site must be evaluated for whether the target code can be modified to cooperate.
Step 3: Apply the replacement for each pattern
- Cancellable work loop: A
常见问题
现代 .NET 还能强制停止线程吗?
不能。现代 .NET 不支持强制终止托管线程,因此应采用协作式取消。
只使用 Thread.Sleep 的代码需要改动吗?
不需要。Join、Sleep 和 Start 的简单用法仍然以相同方式工作,这项技能说明它们不需要迁移。
它也会建议对 Task 进行现代化吗?
它可能会提出可选的改进建议,但会将其与 Thread.Abort 的修复明确区分开。
详情
作者的原始描述
Guides migration of .NET Framework Thread.Abort usage to cooperative cancellation in modern .NET. USE FOR: modernizing code that calls Thread.Abort, catching ThreadAbortException, replacing Thread.ResetAbort, replacing Thread.Interrupt for thread termination, resolving PlatformNotSupportedException or SYSLIB0006 after retargeting to .NET 6+, migrating ASP.NET Response.End or Response.Redirect(url, true) which internally call Thread.Abort. DO NOT USE FOR: code that only uses Thread.Join, Thread.Sleep, or Thread.Start without any abort, interrupt, or ThreadAbortException usage — these APIs work
repo: dotnet/skills
热度
- GitHub 星标
- 5.8k
- 排名
- #14649
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
create-custom-agent
5.8k帮你的智能体创建 VS Code 自定义智能体文件,为每个智能体设置专属工具、指令以及向其他智能体的交接。
create-skill
5.8k帮你的智能体创建新的智能体技能,包括有效的名称、清晰的触发描述,以及符合标准的 SKILL.md 文件。
csharp-scripts
5.8k帮你的智能体通过 .NET 命令行工具以单个文件运行小型 C# 程序,无需创建完整项目。
dotnet-pinvoke
5.8k帮你的智能体在 .NET 中正确调用原生 C 或 C++ 库,涵盖函数签名、字符串处理、内存和安全句柄。