alberthyc
August 3rd, 2007, 12:52 PM
hi, i wrote a mfc dialog that's running a worker thread i want to terminate it from outside.
in CMyTestDlg: public CDialog{
static bool STOP;//a shared variable to signal the thread function
static UINT __cdecl Start();
CWinThread* pThread
....
in the cpp file, i have a OnBnClickedButtonStart() and OnBnClickedButtonStop()function to start and stop the thread
bool CMyTestDlg:: Stop=false;
void CMyTestDlg:: OnBnClickedButtonStart(){
pThread=AfxBeginThread(Start,this,-1,0,CREATE_SUSPENDED);
pThread->m_bAutoDelete=FALSE;
pThread->ResumeThread();
}
void CMyTestDlg:: OnBnClickedButtonStop()
{
STOP=true;
if(:: WaitForSingleObject(pThread->m_hThread,2000)==WAIT_OBJECT_0){
delete pThread;
m_ListBox_MSG.AddString(_T("Thread terminated succesfully"));
//message showed in a list box
}
else{
:: TerminateThread(pThread->m_hThread,0);
m_ListBox_MSG.AddString(_T("Thread is not normally terminated!"));
//message showed in a list box
}
}
UINT __cdecl CMyTestDlg:: Start(LPVOID pParam){
CMyTestDlg* me=(CMyTestDlg*)pParam;
while(STOP==false){
//looping here...
}
m_ListBox_Msg.AddString(_T("ERROR!"));//msg showed when thread function terminates
return 0;
}
when i stop the thread, from the message showed in the listbox, i found that the thread is always terminated "dangeroursly" which means the ::TerminateThread()function ran.
the msg: m_ListBox_Msg.AddString(_T("ERROR!")) is never showed, which means the loop didn't exit.
and i think it's not that i didn't wait long enough, i had a counter in my loop and increase to 1000 in 2 seconds, so i think it shouldn't take that long for the loop to terminate.
What could be the possible cause?
in CMyTestDlg: public CDialog{
static bool STOP;//a shared variable to signal the thread function
static UINT __cdecl Start();
CWinThread* pThread
....
in the cpp file, i have a OnBnClickedButtonStart() and OnBnClickedButtonStop()function to start and stop the thread
bool CMyTestDlg:: Stop=false;
void CMyTestDlg:: OnBnClickedButtonStart(){
pThread=AfxBeginThread(Start,this,-1,0,CREATE_SUSPENDED);
pThread->m_bAutoDelete=FALSE;
pThread->ResumeThread();
}
void CMyTestDlg:: OnBnClickedButtonStop()
{
STOP=true;
if(:: WaitForSingleObject(pThread->m_hThread,2000)==WAIT_OBJECT_0){
delete pThread;
m_ListBox_MSG.AddString(_T("Thread terminated succesfully"));
//message showed in a list box
}
else{
:: TerminateThread(pThread->m_hThread,0);
m_ListBox_MSG.AddString(_T("Thread is not normally terminated!"));
//message showed in a list box
}
}
UINT __cdecl CMyTestDlg:: Start(LPVOID pParam){
CMyTestDlg* me=(CMyTestDlg*)pParam;
while(STOP==false){
//looping here...
}
m_ListBox_Msg.AddString(_T("ERROR!"));//msg showed when thread function terminates
return 0;
}
when i stop the thread, from the message showed in the listbox, i found that the thread is always terminated "dangeroursly" which means the ::TerminateThread()function ran.
the msg: m_ListBox_Msg.AddString(_T("ERROR!")) is never showed, which means the loop didn't exit.
and i think it's not that i didn't wait long enough, i had a counter in my loop and increase to 1000 in 2 seconds, so i think it shouldn't take that long for the loop to terminate.
What could be the possible cause?