简体   繁体   English

C#和.net中的ExitThread(ExitCode)和GetExitCodeThread相当于什么?

[英]What's the equivalent of ExitThread(ExitCode) and GetExitCodeThread in C# & .net?

Reading the VS2008 help file I've figured out that the clean way of exiting a thread (in .net) is either by using a return statement (in C#) or letting the thread reach the end of the method. 阅读VS2008帮助文件我已经发现退出线程(在.net中)的简洁方法是使用return语句(在C#中)或让线程到达方法的末尾。

However, I have not found a method or property that would allow me to set the thread's exit code nor a way to retrieve it (as it is done using the Win32 API). 但是,我还没有找到允许我设置线程退出代码的方法或属性,也没有找到检索它的方法(因为它是使用Win32 API完成的)。 Therefore, the question is, how do I do this using C# and .net ? 因此,问题是,我如何使用C#和.net做到这一点?

Thank you for your help, 谢谢您的帮助,

John. 约翰。

The reason the underlying Win32 thread primitives aren't exposed is to prevent managed code from relying on them. 未公开底层Win32线程基元的原因是为了防止托管代码依赖它们。 The CLR team is always working on ways to optimize thread usage, and that includes no guarantees about 1:1 managed:unmanaged thread mapping (see "Note" on this MSDN page , for instance). CLR团队一直致力于优化线程使用的方法,并且不包括对1:1托管:非托管线程映射的保证(例如,请参阅此MSDN页面上的“注意”)。 If you really want to do it anyway, you'll need to set up P/Invoke wrappers that use an unmanaged thread handle from Win32 GetCurrentThread(), or hook into the thread mapping process yourself with a custom host. 无论如何你真的想要这样做,你需要设置使用Win32 GetCurrentThread()的非托管线程句柄的P / Invoke包装器,或者使用自定义主机自己挂钩到线程映射过程。 I wouldn't recommend either, unless you absolutely have to interop with something that uses thread exit codes and isn't managed code-aware. 我也不建议,除非你绝对必须与使用线程退出代码的东西互操作,而不是托管代码感知。 Figure out another way to smuggle state info around if you can do it all managed (or use Task Parallel Library to abstract up a level from bare threads). 找出另一种走私状态信息的方法,如果你可以全部管理(或使用任务并行库从裸线抽象一个级别)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM