I HATE THREADS
Not really; It's just that I guess I am too stupid to figure them out.
I have a communications app that has 2 threads for Send and Receive. I've
worked and worked and debugged to try and get the logic right to handle
closing the app and the com port logically so as not to get hung. I use
PostMessage() in the threads so as not to get stuck when sending a message
to the app in case is't closing down. However my current problem does not
occur when closing the app but during an OnTimer() message in which case I
detect that the device on the com port has stopped responding. When this
occurs in the OnTimer() call I just close the port.
Each thread has an exit event that tells the thread to stop and exit. When I
close the port I set the event and use this code to check for thread
termination.
if ( m_pSendThread )
{
m_pSendThread->m_bDone = TRUE;
m_pSendThread->Exit();
do
{
VERIFY(::GetExitCodeThread(m_pSendThread->m_hThread, &dwStatus));
}while( dwStatus == STILL_ACTIVE );
}
When this occurs, TRACE() information in the threads indicate that the
thread has exited but dwStatus remains STILL_ACTIVE. So I'm stuck in this
loop. Even worse, If I break in the loop with the debugger, and stop
debugging, the debugger stops but the app is still running. I can't
terminate it with task manager and have to reboot to get it stopped.
My question is why does the status above remain STILL_ACTIVE after the
thread exits?
One other thought, the receive thread is Posting received data messages to
the app que until the the com is closed. My OnNotify handler should discard
these once the comm is closed, but I don't know if the message pump might be
running during ::GetExitCodeThread() or at someother time during the
ComClose() process.
Thanks