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...
}
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.
What could be the possible cause