Click to See Complete Forum and Search --> : having problem with terminating a worker thread


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?

Arjay
August 3rd, 2007, 02:17 PM
A couple of things:

1) Instead of using the bool STOP variable, use a Event (or CEvent) instead to signal the thread to exit. Check the event with WaitForSingleObject( hEvent, 0 ) == WAIT_OBJECT_0 to determine if the event has been set.
2) You are not allowed to access MFC UI controls from a secondary thread. Read the MFC FAQ about accessing UI controls from another page (search the forum for the FAQ).

Check out the SimpleThread.zip file in this post (http://www.codeguru.com/forum/showthread.php?p=1607352#post1607352). This other post (http://www.codeguru.com/forum/showthread.php?t=430034&page=1&pp=15&highlight=SimpleThread) refers to the example and a discussion on preferring events over the use of a bool variable. Btw, not that it matters in your example, but using the static bool can lead to issues if you have more that one instance of a class running.

UnderDog
August 10th, 2007, 01:17 PM
Arjay has some good points and those are good things to do.

I am curious though - the code that you posted should work wrt to worker thread termination at least. How can you be sure that one iteration of your loop completes within 2 seconds? What is the code inside that loop?