2008-11-19 41 views

回答

33

MSDN提到“当一个线程终止时,线程对象达到一个信号状态,满足任何在该对象上等待的线程”。

所以,你可以通过检查线程处理的状态检查一个线程是否已经终止 - 无论是信号还是不:

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 
} 
+0

我同意这个解决方案。然而,我使用`code == WAIT_TIMEOUT && ExitCode == STILL_ACTIVE`,在那里我分别使用`DWORD code = WaitForSingleObject(tHwnd,0)`和`GetExitCodeThread(tHwnd,&ExitCode)`定义了代码和ExitCode。 – 2012-12-29 22:37:00

5

您链接的文档使用STILL_ACTIVE作为返回码警告,因为它无法与用于指示活动线程的返回值区分开来。 所以不要用它作为返回值,你不会有这个问题。

+0

卫生署!我完全读错了!感谢您解决这个问题! – Uhall 2008-11-19 05:26:47