gpt4 book ai didi

c# - 在C#中手动设置线程退出代码?

转载 作者:行者123 更新时间:2023-12-03 13:19:11 26 4
gpt4 key购买 nike

有没有一种方法可以在C#中手动设置线程退出代码(出于调试目的)?

相关问题“What is a thread exit code?”的选定答案指出:

0 tends to mean that it exited safely whilst anything else tends to mean it didn't exit as expected. But then this exit code can be set in code by yourself to completely overlook this.



真的有办法自己设置线程的退出代码吗?

最佳答案

.NET线程没有退出代码。这些由Windows上的 native 线程使用,但 native 线程仅由托管线程使用,并且与给定的托管线程没有1:1对应关系。同一托管线程可以在多个 native 线程上运行,反之亦然(尽管显然不能同时运行)。引用MSDN:

An operating-system ThreadId has no fixed relationship to a managed thread, because an unmanaged host can control the relationship between managed and unmanaged threads. Specifically, a sophisticated host can use the Fiber API to schedule many managed threads against the same operating system thread, or to move a managed thread among different operating system threads.



当然,这适用于与 native 线程相关的所有资源-但是运行时确实可以管理线程的托管资源;当然,运行时也可以管理线程的托管资源。对于非托管代码调用托管代码,线程将保持不变-否则互操作将非常不可能。

如果您想为任务添加额外的信息,请尝试使用更高级别的抽象-例如 Task。是否需要在完成时输出任务状态?添加一个延续。需要检查您引用的任务的状态吗?等待它或查询 Task对象。

关于c# - 在C#中手动设置线程退出代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42088707/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com