C# 什么是线程退出代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18887864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a thread exit code?
提问by Ondrej Janacek
What exactly is a thread exit code in the Output window while debugging? What information it gives me? Is it somehow useful or just an internal stuff which should not bother me?
调试时输出窗口中的线程退出代码到底是什么?它给了我什么信息?它以某种方式有用还是只是不应该打扰我的内部内容?
The thread 0x552c has exited with code 259 (0x103).
The thread 0x4440 has exited with code 0 (0x0).
Is there maybe some sort of list of possible exit codes along with its significance?
是否有某种可能的退出代码列表及其重要性?
采纳答案by Sayse
There actually doesn't seem to be a lot of explanation on this subject apparently but the exit codes are supposed to be used to give an indication on how the thread exited, 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.
实际上似乎没有关于这个主题的很多解释,但退出代码应该用于指示线程如何退出,0
往往意味着它安全退出,而其他任何东西往往意味着它没有没有按预期退出。但是这个退出代码可以自己在代码中设置以完全忽略这一点。
The closest link I could find to be useful for more information is this
我能找到的对更多信息有用的最接近的链接是这个
Quote from above link:
引用上面的链接:
What ever the method of exiting, the integer that you return from your process or thread must be values from 0-255(8bits). A zero value indicates success, while a non zero value indicates failure. Although, you can attempt to return any integer value as an exit code, only the lowest byte of the integer is returned from your process or thread as part of an exit code. The higher order bytes are used by the operating system to convey special information about the process. The exit code is very useful in batch/shell programs which conditionally execute other programs depending on the success or failure of one.
无论退出的方法是什么,您从进程或线程返回的整数必须是 0-255(8 位)之间的值。零值表示成功,而非零值表示失败。虽然您可以尝试返回任何整数值作为退出代码,但只有整数的最低字节作为退出代码的一部分从您的进程或线程中返回。操作系统使用高位字节来传达有关进程的特殊信息。退出代码在批处理/外壳程序中非常有用,这些程序根据一个程序的成功或失败有条件地执行其他程序。
From the Documentation for GetEXitCodeThread
来自GetEXitCodeThread的文档
Important The GetExitCodeThread function returns a valid error code defined by the application only after the thread terminates. Therefore, an application should not use STILL_ACTIVE (259) as an error code. If a thread returns STILL_ACTIVE (259)as an error code, applications that test for this value could interpret it to mean that the thread is still running and continue to test for the completion of the thread after the thread has terminated, which could put the application into an infinite loop.
重要 GetExitCodeThread 函数仅在线程终止后返回由应用程序定义的有效错误代码。因此,应用程序不应使用 STILL_ACTIVE (259) 作为错误代码。如果线程返回 STILL_ACTIVE (259)作为错误代码,则测试此值的应用程序可能会将其解释为线程仍在运行并在线程终止后继续测试线程是否完成,这可能会导致应用进入无限循环。
My understanding of all this is that the exit code doesn't matter all that much if you are using threads within your own application foryour own application. The exception to this is possibly if you are running a couple of threads at the same time that have a dependency on each other. If there is a requirement for an outside source to read this error code, then you can set it to let other applications know the status of your thread.
我对这一切的理解是,如果您在自己的应用程序中为自己的应用程序使用线程,则退出代码并不重要。如果您同时运行几个相互依赖的线程,则可能例外。如果需要外部源读取此错误代码,则可以将其设置为让其他应用程序知道您线程的状态。
回答by Kristian Williams
As Sayse mentioned, exit code 259 (0x103)
has special meaning, in this case the process being debugged is still running.
正如 Sayse 所提到的,退出代码259 (0x103)
具有特殊含义,在这种情况下,被调试的进程仍在运行。
I saw this a lot with debugging web services, because the thread continues to run after executing each web service call (as it is still listening for further calls).
我在调试 Web 服务时看到了很多,因为线程在执行每个 Web 服务调用后继续运行(因为它仍在侦听进一步的调用)。
回答by Feng Zhang
what happened to me is that I have multiple projects in my solution. I meant to debug project 1, however, the project 2 was set as the default starting project. I fixed this by, right click on the project and select "Set as startup project", then running debugging is fine.
发生在我身上的是我的解决方案中有多个项目。我打算调试项目 1,但是,项目 2 被设置为默认启动项目。我解决了这个问题,右键单击项目并选择“设置为启动项目”,然后运行调试就可以了。