1

Is there a way to set the thread exit code manually in C# (for debugging purposes)?

The selected answer of a related question "What is a thread exit code?" states:

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.

Is there really a way to set a thread's exit code myself?

Community
  • 1
  • 1
MathuSum Mut
  • 2,765
  • 3
  • 27
  • 59
  • You shouldn't use/rely on them in .net, reasons/explanation [here](https://stackoverflow.com/questions/5628908/whats-the-equivalent-of-exitthreadexitcode-and-getexitcodethread-in-c-sharp). – Manfred Radlwimmer Feb 07 '17 at 11:32
  • There is a Windows API call [`ExitThread(DWORD)`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682659(v=vs.85).aspx) that exits a thread using the provided exit code. But I'm pretty sure it's a very bad idea to call that from managed threads, for the reasons linked by Manfred – René Vogt Feb 07 '17 at 11:32

1 Answers1

3

.NET threads do not have exit codes. Those are used by the native threads on Windows, but native threads are only used by managed threads, and have no 1:1 correspondence to a given managed thread. The same managed thread can run on multiple native threads and vice versa (though obviously not at the same time). To quote 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.

This of course applies to all resources tied to the native thread - but the runtime does manage the managed resources of a thread, of course; and for unmanaged code calling into managed code, the thread will be kept the same - otherwise interop would be quite impossible.

If you want to add extra information to tasks, try using a higher level of abstraction - e.g. Task. Need to output the status of a task on completion? Add a continuation. Need to check the status of a task you have a reference for? Await it or query the Task object.

Luaan
  • 62,244
  • 7
  • 97
  • 116
  • "The same managed thread can run on multiple native threads", how so? – MathuSum Mut Feb 07 '17 at 11:58
  • @MathuSumMut Why wouldn't it? It isn't tied to native threads in any way. The runtime decides which native threads handle which managed threads. – Luaan Feb 07 '17 at 11:59
  • Why would a managed thread not tie 1:1 with a native thread? It does in order to maintain interoperability. I know you're going by what's *technically* allowed by the spec, but there a no advantages to not mapping 1:1, and indeed, in order to not tie with implementation, there's a separate thread pool for that. – MathuSum Mut Feb 07 '17 at 12:03
  • @MathuSumMut Interoperability is a different scenario - the same native thread is kept tied to the managed thread in that case. There's no benefit to keeping a 1:1 mapping *in general*, and it would limit the runtime severely in scenarios like MS SQL CLR and similar. Don't forget that .NET was fairly ambitious - they didn't want to close any doors to highly scalable systems and other demanding scenarios. – Luaan Feb 07 '17 at 12:06
  • How would the runtime be limited severely? – MathuSum Mut Feb 07 '17 at 12:12
  • @MathuSumMut It would be required to use native threads for things that could better be handled by fibers, for example. Native threads have quite a bit of overhead - you don't want to create thousands of them, especially on 32-bit systems. The threadpool helps with that, but doesn't preserve the identity/execution context of the thread, and can easily grow when used improperly. Mind you, the newer approaches like `Task` make this less necessary than before, with their cheap "pseudo-threads", but those weren't there in .NET 1.0. – Luaan Feb 07 '17 at 12:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135065/discussion-between-mathusum-mut-and-luaan). – MathuSum Mut Feb 07 '17 at 12:17