Click to See Complete Forum and Search --> : Invalid thread handle


HTS
October 11th, 2007, 08:51 AM
Hello, I am creating a thread (let's call it thread B) from within another thread (thread A) , and saving the thread handle returned by AfxBeginThread. Before returning from thread A, I wait for thread B to terminate, using WaitForSingleObject with the thread handle. Problem is: if thread B already terminated (it terminates when the thread function returns, not by endthread) by the time I call WaitForSingleObject, this routine fails, returning WAIT_FAILED, and when I call GetLastError I get error no. 6: "The handle is invalid." Since I have handles leak, I suspect that the handle is not really closed (there is no point to call CloseHandle on it, if it's invalid). Any idea why the handle becomes invalid? maybe the fact that I create the thread from withing another thread is problematic? thanks

UnderDog
October 16th, 2007, 02:26 AM
CWinThread invalidates the handle when the thread ceases to exist. That would explain it.