XSLT存档  

不及格的程序员-八神

 查看分类:  ASP.NET XML/XSLT JavaScripT   我的MSN空间Blog
Asked 14 years, 2 months ago
 

How can I determine if a Win32 thread has terminated?

The documentation for GetExitCodeThread warns to not to use it for this reason since the error code STILL_ACTIVE can be returned for other reasons.

Thanks for the help! :)

 
  •  
    If the thread is your own you can make sure that all your threads return 0. So this problem will never arise. I think ExitCodeThread() is the better solution than the accepted answer in case you are needing this for your own threads because the "other reasons" mentioned in the MSDN are simply that YOU return the exit code STILL_ACTIVE from your thread routine. So simply don't do it. 
    – Elmue
     Aug 30, 2018 at 14:15 

2 Answers

7

The documentation you link to warns against using STILL_ACTIVE as a return code, since it can't be distinguished from the return value used to indicate an active thread. So don't use it as a return value and you won't have this problem.

 
 
40
 

MSDN mentions that "When a thread terminates, the thread object attains a signaled state, satisfying any threads that were waiting on the object".

So, you can check for whether a thread has terminated by checking the state of the thread handle - whether it's signaled or not:

DWORD result = WaitForSingleObject( hThread, 0);

if (result == WAIT_OBJECT_0) {
    // the thread handle is signaled - the thread has terminated
}
else {
    // the thread handle is not signaled - the thread is still alive
}
 
posted on 2023-02-02 22:52  不及格的程序员-八神  阅读(12)  评论(0编辑  收藏  举报