Thanks Quantum1024,
that was some helpful piece of info. It works if I want to kill the thread while the program is still running. But if a user clicks on the Exit button "X", I would like my program to make sure the thread returns before the program exits.(If I don't it gives me memory leaks again) I tried putting a while loop right after I use PostThreadMessage() as shown below:
Code:
if(m_hProgressThread != NULL && !PostThreadMessage(m_connectThreadID, WM_QUIT, 0, 0)) {
AfxMessageBox("Progress thread could not be terminated");
}
while(!threadEnded) {
::Sleep(1000);
}
threadEnded = true;
CloseHandle(m_hProgressThread); // Reset the progress bar thread handle
m_hProgressThread = NULL;
m_connectThreadID = 0;
The while loop waits on threadEnded which gets set by the thread just before it does return 0; as shown below:
Code:
DWORD WINAPI CVideo::VideoFreeSpaceThreadHandler(LPVOID pSimulatorDlg)
{
pThis->threadEnded = false; // Used to notify the outside that this thread ended.
while(!exitThisThread) {
MSG Msg;
if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
{
if (Msg.message==WM_QUIT) {
exitThisThread = true;
}
}
DO SOMETHING USEFUL
Sleep(1000); // Make the thread sleep for 1 second before repeating
}
pThis->threadEnded = true;
return 0;
}
Alas the program hangs when I do that. Is there a more efficient way of doing this. Thank you for any help you can provide
Amish