Re: how to end a thread from the main thread

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Joseph M. Newcomer wrote:
The trick goes like this...

Suppose for this example I have a boolean, ThreadRunning, on behalf of a view.  Also a
boolean called closing.  Both are initialized to false, and ThreadRunning is set to TRUE
when the thread starts

void CMyView::OnClose()
{
if(ThreadRunning)
{
... initiate thread shutdown
closing = TRUE;
} CView::OnClose();
}



LRESULT CMyView::OnThreadFinished(WPARAM, LPARAM) { ThreadRunning = FALSE; if(closing) PostMessage(WM_CLOSE); return 0; }


Don't you need a variable isClosed and set it true as the last thing the thread does so the application can wait for it (while(!isClosed)Sleep(10);) to be closed before exiting?
.