Tags

,

CWinThread* m_pThread;

// Creating Thread
if (NULL == m_pThread)
m_pThread = AfxBeginThread(ThreadProc,
NULL,
THREAD_PRIORITY_NORMAL,
0,
CREATE_SUSPENDED,
NULL);

m_pThread->m_bAutoDelete = false;
m_pThread->ResumeThread();

TerminateThread forces another thread to exit. You should avoid calling it at all costs as it will stop a thread dead in it’s tracks without any chance to cleanup. This includes any CRT memory allocated.

ExitThread is for the currently running thread to stop itself nice and cleanly. When you called it above, you likely forced the main (UI) thread to exit and likely left the running threads still lingering around. Hence, your program was still running as evidenced in Task Manager. GetExitCodeThread was also likely failing since the threads had not actually exited.

But the right way to stop a thread is to cleanly signal by any clean means necessary that it should exit. Then allow the threads to exit on their own before allowing the main thread to exit.

// Deleting thread
WaitForSingleObject(m_pThread->m_hThread, INFINITE);

// get the thread's exit code (I'm not sure why you need it)
DWORD dwExitCode;
BOOL bRet = GetExitCodeThread(m_pThread->m_hThread, &dwExitCode);

// cleanup the thread
CloseHandle(m_pThread->m_hThread);
m_pThread->m_hThread = NULL;

delete m_pThread;
m_pThread = NULL;

Advertisements