wait for CWinThread to terminate -- from UI thread
-
How do I terminate my CWinThread from my main UI thread -- without blocking anything. I was thinking I'd try something like: PostThreadMessage( WM_QUIT, 0, 0 ); MSG msg; while( PeekMessage(&msg, NULL, 0,0, PM_REMOVE ) ) { if ( WaitForSingleObject(m_hThread,0) == WAIT_OBJECT_0 ) break; TranslateMessage(&msg); DispatchMessage(&msg); } But that doesn't seem to work... Any suggestions?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)
-
How do I terminate my CWinThread from my main UI thread -- without blocking anything. I was thinking I'd try something like: PostThreadMessage( WM_QUIT, 0, 0 ); MSG msg; while( PeekMessage(&msg, NULL, 0,0, PM_REMOVE ) ) { if ( WaitForSingleObject(m_hThread,0) == WAIT_OBJECT_0 ) break; TranslateMessage(&msg); DispatchMessage(&msg); } But that doesn't seem to work... Any suggestions?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)
or what about: if I set m_bAutoDelete to FALSE and: PostThreadMessage( WM_QUIT, 0, 0 ); while ( PumpMessage() ) { if ( WaitForSingleObject(m_hThread,0) == WAIT_OBJECT_0 ) break; } delete this;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)
-
How do I terminate my CWinThread from my main UI thread -- without blocking anything. I was thinking I'd try something like: PostThreadMessage( WM_QUIT, 0, 0 ); MSG msg; while( PeekMessage(&msg, NULL, 0,0, PM_REMOVE ) ) { if ( WaitForSingleObject(m_hThread,0) == WAIT_OBJECT_0 ) break; TranslateMessage(&msg); DispatchMessage(&msg); } But that doesn't seem to work... Any suggestions?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)
An example: In your class which creates the thread: private: HANDLE m_StopThread; HANDLE m_WaitThread; static UINT ThreadFunction(LPVOID pvParam); When you create your thread: // Create events m_StopThread = CreateEvent(0, TRUE, FALSE, 0); m_WaitThread = CreateEvent(0, TRUE, FALSE, 0); // Start thread AfxBeginThread(ThreadFunction, this); When you want to terminate the thread: // Trigger thread to stop ::SetEvent(m_StopThread); // Wait until thread finished ::WaitForSingleObject(m_WaitThread, INFINITE); // Close handles ::CloseHandle(m_StopThread); ::CloseHandle(m_WaitThread); //Your thread procedure UINT CFoo::ThreadFunction(LPVOID* pvParam) { CFoo *pParent = static_cast(pvParam); while(true) { // Check event for stop thread if(::WaitForSingleObject(pParent->m_StopThread, 0) == WAIT_OBJECT_0) { // Set event ::SetEvent(pParent->m_WaitThread); return 0; } // Do your processing } } Hope this helps you out!
-
How do I terminate my CWinThread from my main UI thread -- without blocking anything. I was thinking I'd try something like: PostThreadMessage( WM_QUIT, 0, 0 ); MSG msg; while( PeekMessage(&msg, NULL, 0,0, PM_REMOVE ) ) { if ( WaitForSingleObject(m_hThread,0) == WAIT_OBJECT_0 ) break; TranslateMessage(&msg); DispatchMessage(&msg); } But that doesn't seem to work... Any suggestions?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)
That will only work if the thread has a message pump - i.e. is created as a UI thread. If it is a worker thread, I usually do the following (error checking removed for the sake of brevity): in the main UI thread:
// create worker thread
m_hTermEvent = CreateEvent (NULL, TRUE, FALSE, NULL);
m_pThread = AfxBeginThread (ThreadFunction, &m_hTermEvent);// time to terminate worker thread
SetEvent (m_hTermEvent);
WaitForSingleObject (m_pThread->m_hThread, INFINITE);in the worker thread:
UINT __cdecl ThreadFunction( LPVOID pParam )
{
HANDLE hEvent;hEvent = \*((HANDLE \*) pParam); while (true) { if WaitForSingleObject (hEvent, 0) == WAIT\_OBJECT\_0) break; // do your work }; return 0;
}
Judy
-
That will only work if the thread has a message pump - i.e. is created as a UI thread. If it is a worker thread, I usually do the following (error checking removed for the sake of brevity): in the main UI thread:
// create worker thread
m_hTermEvent = CreateEvent (NULL, TRUE, FALSE, NULL);
m_pThread = AfxBeginThread (ThreadFunction, &m_hTermEvent);// time to terminate worker thread
SetEvent (m_hTermEvent);
WaitForSingleObject (m_pThread->m_hThread, INFINITE);in the worker thread:
UINT __cdecl ThreadFunction( LPVOID pParam )
{
HANDLE hEvent;hEvent = \*((HANDLE \*) pParam); while (true) { if WaitForSingleObject (hEvent, 0) == WAIT\_OBJECT\_0) break; // do your work }; return 0;
}
Judy
That is NOT the case. This works as it should for worker threads! Please, show me what in my code would not work!
-
That is NOT the case. This works as it should for worker threads! Please, show me what in my code would not work!
pierre_ribery wrote:
That is NOT the case. This works as it should for worker threads! Please, show me what in my code would not work!
Calm down dude. No need to get snippy. I wasn't responding to your post, I was responding to the original poster. The presence of my post does not imply that yours is wrong. If you'll check the time stamp, we were posting at the same time. Your post didn't exist when I started mine. Judy
-
That is NOT the case. This works as it should for worker threads! Please, show me what in my code would not work!
Maybe I should go back to basic threading. CWinThread is a UI thread in this case -- and has a message pump. It seems that if I wait for it to terminate, then I am blocking my UI thread. So, it seems I need to pump messages while I wait. Or, do I have no clue? More likely, I am just making this harder than it needs to be. But, the goal is to wait for this thing to terminate completely.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)
-
How do I terminate my CWinThread from my main UI thread -- without blocking anything. I was thinking I'd try something like: PostThreadMessage( WM_QUIT, 0, 0 ); MSG msg; while( PeekMessage(&msg, NULL, 0,0, PM_REMOVE ) ) { if ( WaitForSingleObject(m_hThread,0) == WAIT_OBJECT_0 ) break; TranslateMessage(&msg); DispatchMessage(&msg); } But that doesn't seem to work... Any suggestions?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)
Are you terminating a UI thread from within itself as you're showing in your code or do you want to terminate a thread from another thread and wait? For the latter, maybe something like this:
// Called on main thread to terminate OtherCWinThread (a CWinThread object, UI thread)
OtherCWinThread.PostThreadMessage(WM_QUIT, 0, 0);
while (1)
{
DWORD dwWaitRet = ::MsgWaitForMultipleObjects(1, &OtherCWinThread.m_hThread, FALSE, INFINITE, QS_ALLEVENTS);if (dwWaitRet != WAIT_OBJECT_0 + 1)
break;MSG msg;
while ( ::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
AfxGetApp()-> PumpMessage();
}
}mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Are you terminating a UI thread from within itself as you're showing in your code or do you want to terminate a thread from another thread and wait? For the latter, maybe something like this:
// Called on main thread to terminate OtherCWinThread (a CWinThread object, UI thread)
OtherCWinThread.PostThreadMessage(WM_QUIT, 0, 0);
while (1)
{
DWORD dwWaitRet = ::MsgWaitForMultipleObjects(1, &OtherCWinThread.m_hThread, FALSE, INFINITE, QS_ALLEVENTS);if (dwWaitRet != WAIT_OBJECT_0 + 1)
break;MSG msg;
while ( ::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
AfxGetApp()-> PumpMessage();
}
}mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Thank you! -- I knew someone would understand the question!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)
-
Are you terminating a UI thread from within itself as you're showing in your code or do you want to terminate a thread from another thread and wait? For the latter, maybe something like this:
// Called on main thread to terminate OtherCWinThread (a CWinThread object, UI thread)
OtherCWinThread.PostThreadMessage(WM_QUIT, 0, 0);
while (1)
{
DWORD dwWaitRet = ::MsgWaitForMultipleObjects(1, &OtherCWinThread.m_hThread, FALSE, INFINITE, QS_ALLEVENTS);if (dwWaitRet != WAIT_OBJECT_0 + 1)
break;MSG msg;
while ( ::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
AfxGetApp()-> PumpMessage();
}
}mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
ah! insight! I never quite made the distinction. Thanks!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)