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

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


The reason the underlying Win32 thread primitives aren't exposed is to prevent managed code from relying on them. 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). 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. 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).