WaitForSingleObject blocks while waiting for the CWinThread
Posted on 2006-05-31
Hi, i have a dialog based MFC application and i am using CListCtrl to populate the list. As the number of items in the list are too high, i am using a CWinThread to handle this. Below is the summary of actions that are being performed
1) From OnInitDialog i create a CWinThread(say m_listCtrlThread) and i resume it soon after that. I am using a flag called
2) The thread starts running and I see my CListCtrl being populated.
3) While it is being populated I want to exit from my dialog by clicking the Ok button. But my main application blocks while waiting for the subordinate thread. I have used both WaitForSingleObject,MsgWaitForMultipleObjects. Can someone tell me where I am going wrong. Below is the sample code.....
isokToExit = false; //this is a member flag which will be used later
//create CWinThread(m_listCtrlThread) and resume it soon after it in MyThreadProc
MyDialog *dlg = (MyDDialog *)b;
if(!isokToExit) //This flag will be set when we press the OnOK button
//keep on showing the list 20 records at a time and after every 20 records sleep(200);
isokToExit = true; //Set the flag to true so that we break from the ShowListCtrl loop immediately
WaitForSingleObject(m_listCtrlThread->m_hThread,INFINITE); //This call blocks here and never returns, i donot want to return by putting some timeout period
//MsgWaitForMultipleObjects(1,&(m_listCtrlThread->m_hThread),FALSE,INFINITE,QS_ALLEVENTS); //This doesnot return either