2008-11-19 69 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