Help on multi-threading
-
I'm studying multi-threading today and got some major problems, the biggest one is that the worker thread is not terminating as expected, my code looks like this:
// global BOOL g_bAbort = FALSE; UINT ThreadProc(LPVOID lpParam) { while (!g_bAbort) { // DoSomething(); // do some thing } return 0; } // in some class named "CThreadTestDlg" void CThreadTestDlg::OnStart() { // begin the thread g_bAbort = FALSE; m_pThread = ::AfxBeginThread(ThreadProc, NULL); } void CThreadTestDlg::OnStop() { // wanna stop the thread... g_bAbort = TRUE; DWORD dwExitCode = 0; do { Sleep(200); ::GetExitCodeThread(m_pThread->m_hThread, &dwExitCode); } while (dwExitCode == STILL_ACTIVE); }
OnStart() is called as soon as the program starts, OnStop() is called when a button captioned ""stop" is pressed. I press the "stop" button and my program freezes. I used debugger and found out that the thread never ended, even though g_bAbort has been set to TRUE. Why so? Thank you a lot. -
I'm studying multi-threading today and got some major problems, the biggest one is that the worker thread is not terminating as expected, my code looks like this:
// global BOOL g_bAbort = FALSE; UINT ThreadProc(LPVOID lpParam) { while (!g_bAbort) { // DoSomething(); // do some thing } return 0; } // in some class named "CThreadTestDlg" void CThreadTestDlg::OnStart() { // begin the thread g_bAbort = FALSE; m_pThread = ::AfxBeginThread(ThreadProc, NULL); } void CThreadTestDlg::OnStop() { // wanna stop the thread... g_bAbort = TRUE; DWORD dwExitCode = 0; do { Sleep(200); ::GetExitCodeThread(m_pThread->m_hThread, &dwExitCode); } while (dwExitCode == STILL_ACTIVE); }
OnStart() is called as soon as the program starts, OnStop() is called when a button captioned ""stop" is pressed. I press the "stop" button and my program freezes. I used debugger and found out that the thread never ended, even though g_bAbort has been set to TRUE. Why so? Thank you a lot.CWinThread normally has m_bAutoDelete set TRUE which means it will delete itself and close the m_hThread handle upon termination. You should either reset the m_bAutoDelete flag and delete it yourself or else use DuplicateHandle to create your own copy of the m_hThread handle which will be valid in your OnStop function.
-
CWinThread normally has m_bAutoDelete set TRUE which means it will delete itself and close the m_hThread handle upon termination. You should either reset the m_bAutoDelete flag and delete it yourself or else use DuplicateHandle to create your own copy of the m_hThread handle which will be valid in your OnStop function.
-
What's the return value of GetExitCodeThread?
-
I'm studying multi-threading today and got some major problems, the biggest one is that the worker thread is not terminating as expected, my code looks like this:
// global BOOL g_bAbort = FALSE; UINT ThreadProc(LPVOID lpParam) { while (!g_bAbort) { // DoSomething(); // do some thing } return 0; } // in some class named "CThreadTestDlg" void CThreadTestDlg::OnStart() { // begin the thread g_bAbort = FALSE; m_pThread = ::AfxBeginThread(ThreadProc, NULL); } void CThreadTestDlg::OnStop() { // wanna stop the thread... g_bAbort = TRUE; DWORD dwExitCode = 0; do { Sleep(200); ::GetExitCodeThread(m_pThread->m_hThread, &dwExitCode); } while (dwExitCode == STILL_ACTIVE); }
OnStart() is called as soon as the program starts, OnStop() is called when a button captioned ""stop" is pressed. I press the "stop" button and my program freezes. I used debugger and found out that the thread never ended, even though g_bAbort has been set to TRUE. Why so? Thank you a lot.In the OnStart handler, you should start the thread, then loop the PeekMessage function until the thread exits or the user presses STOP.
void CThreadTestDlg::OnStart(){
// begin the thread
MSG msg;
DWORD threadec;
g_bAbort = FALSE;
m_pThread = ::AfxBeginThread(ThreadProc, NULL);
while(::PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
// process any messages while the other thread does background
// processing
if(msg.message==WM_QUIT){ // quit this thread.
// Add code to break the other thread here.
::PostQuitMessage(msg.wParam);
break;
}
::TranslateMessage(&msg);
::DispatchMessage(&msg);
if(!::GetExitCodeThread(m_pThread->m_hThread,&threadec))
return;
if(threadec!=STILL_ACTIVE)break;
}
}I apologize it this isn't right; I'm much used to programming in C. Peter O.
-
What's the return value of GetExitCodeThread?
-
I'm studying multi-threading today and got some major problems, the biggest one is that the worker thread is not terminating as expected, my code looks like this:
// global BOOL g_bAbort = FALSE; UINT ThreadProc(LPVOID lpParam) { while (!g_bAbort) { // DoSomething(); // do some thing } return 0; } // in some class named "CThreadTestDlg" void CThreadTestDlg::OnStart() { // begin the thread g_bAbort = FALSE; m_pThread = ::AfxBeginThread(ThreadProc, NULL); } void CThreadTestDlg::OnStop() { // wanna stop the thread... g_bAbort = TRUE; DWORD dwExitCode = 0; do { Sleep(200); ::GetExitCodeThread(m_pThread->m_hThread, &dwExitCode); } while (dwExitCode == STILL_ACTIVE); }
OnStart() is called as soon as the program starts, OnStop() is called when a button captioned ""stop" is pressed. I press the "stop" button and my program freezes. I used debugger and found out that the thread never ended, even though g_bAbort has been set to TRUE. Why so? Thank you a lot.I think you should do like this... void CThreadTestDlg::OnStop() { // wanna stop the thread... g_bAbort = TRUE; DWORD dwExitCode = 0; } Thus you can abort the thread. No pains, no gains.
-
I think you should do like this... void CThreadTestDlg::OnStop() { // wanna stop the thread... g_bAbort = TRUE; DWORD dwExitCode = 0; } Thus you can abort the thread. No pains, no gains.
I think you should do like this... void CThreadTestDlg::OnStop() { // wanna stop the thread... g_bAbort = TRUE; } Thus you can abort the thread. No pains, no gains.
-
I'm studying multi-threading today and got some major problems, the biggest one is that the worker thread is not terminating as expected, my code looks like this:
// global BOOL g_bAbort = FALSE; UINT ThreadProc(LPVOID lpParam) { while (!g_bAbort) { // DoSomething(); // do some thing } return 0; } // in some class named "CThreadTestDlg" void CThreadTestDlg::OnStart() { // begin the thread g_bAbort = FALSE; m_pThread = ::AfxBeginThread(ThreadProc, NULL); } void CThreadTestDlg::OnStop() { // wanna stop the thread... g_bAbort = TRUE; DWORD dwExitCode = 0; do { Sleep(200); ::GetExitCodeThread(m_pThread->m_hThread, &dwExitCode); } while (dwExitCode == STILL_ACTIVE); }
OnStart() is called as soon as the program starts, OnStop() is called when a button captioned ""stop" is pressed. I press the "stop" button and my program freezes. I used debugger and found out that the thread never ended, even though g_bAbort has been set to TRUE. Why so? Thank you a lot.- you should use WaitForSingleObject instead of Sleep/GetExitCodeThread in OnStop. 2) try marking g_bAbort with 'volatile' modifier Tomasz Sowinski -- http://www.shooltz.com
- It's for protection
- Protection from what? Zee Germans?