Saul775
2006-11-16 17:23:02 UTC
Hello, all:
I'm still getting accustomed to the idea of threads having a message queue,
but I'm trying to work with CWinThread because of the power of classes.
I have a class derived from CWinThread. It does background work for my main
thread. However, I'm trying to have it exit NICELY. By this, I mean I'd
like it to finish its last calculation before exiting. I've read many
articles and such on the Internet, but I have yet to see a consensus on how
to accomplish this.
Here is some sample code...
#define WM_BEGINCALC WM_APP + 0;
CMyThread::CMyThread()
{
this->m_hStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
this->m_hOKToExitEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
}
VOID CMyThread::OnBeginCalc(WPARAM wParam, LPARAM lParam)
{
BOOL bDone = FALSE;
while (!bDone)
{
if (WaitForSingleObject(this->m_hStopEvent, 0) == WAIT_OBJECT_0)
{
bDone = TRUE;
SetEvent(this->m_hOKToExitEvent);
}
else
{
... // Perform an epoch of calculations
}
}
}
I create my thread as follows...
CMyThread *pThread;
pThread = (CMyThread *)AfxBeginThread(RUNTIME_CLASS(CMyThread),
THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
pThread->m_bAutoDelete = FALSE;
pThread->Initialize(...); // This just initializes some other variables I
need
pThread->ResumeThread();
pThread->BeginCalc(); // This does PostThreadMessage(WM_BEGINCALC, 0, 0)
Great! The thread is running!
However, now I want to stop the thread, but I want to stop it nicely. I've
tried capturing the WM_QUIT in PreProcessMessage(MSG *pMsg) as follows...
BOOL CDAQThread::PreTranslateMessage(MSG *pMsg)
{
if (pMsg->message == WM_QUIT)
{
SetEvent(this->m_hStopEvent); // This event stops the epoch
WaitForSingleObject(this->m_hOKToExitEvent, INFINITE);
}
return CWinThread::PreTranslateMessage(pMsg);
}
As such, I try stopping in the main thread as follows...
pThread->PostThreadMessage(WM_QUIT, 0, 0);
WaitForSingleObject(pThread->m_hThread, INFINITE);
Well, the thread never stops.
How can I make it that the thread will stop NICELY, but still finish it's
last epoch in the loop? Perhaps there is a better way to accomplish this?
Thank you, all.
Saul775
I'm still getting accustomed to the idea of threads having a message queue,
but I'm trying to work with CWinThread because of the power of classes.
I have a class derived from CWinThread. It does background work for my main
thread. However, I'm trying to have it exit NICELY. By this, I mean I'd
like it to finish its last calculation before exiting. I've read many
articles and such on the Internet, but I have yet to see a consensus on how
to accomplish this.
Here is some sample code...
#define WM_BEGINCALC WM_APP + 0;
CMyThread::CMyThread()
{
this->m_hStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
this->m_hOKToExitEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
}
VOID CMyThread::OnBeginCalc(WPARAM wParam, LPARAM lParam)
{
BOOL bDone = FALSE;
while (!bDone)
{
if (WaitForSingleObject(this->m_hStopEvent, 0) == WAIT_OBJECT_0)
{
bDone = TRUE;
SetEvent(this->m_hOKToExitEvent);
}
else
{
... // Perform an epoch of calculations
}
}
}
I create my thread as follows...
CMyThread *pThread;
pThread = (CMyThread *)AfxBeginThread(RUNTIME_CLASS(CMyThread),
THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
pThread->m_bAutoDelete = FALSE;
pThread->Initialize(...); // This just initializes some other variables I
need
pThread->ResumeThread();
pThread->BeginCalc(); // This does PostThreadMessage(WM_BEGINCALC, 0, 0)
Great! The thread is running!
However, now I want to stop the thread, but I want to stop it nicely. I've
tried capturing the WM_QUIT in PreProcessMessage(MSG *pMsg) as follows...
BOOL CDAQThread::PreTranslateMessage(MSG *pMsg)
{
if (pMsg->message == WM_QUIT)
{
SetEvent(this->m_hStopEvent); // This event stops the epoch
WaitForSingleObject(this->m_hOKToExitEvent, INFINITE);
}
return CWinThread::PreTranslateMessage(pMsg);
}
As such, I try stopping in the main thread as follows...
pThread->PostThreadMessage(WM_QUIT, 0, 0);
WaitForSingleObject(pThread->m_hThread, INFINITE);
Well, the thread never stops.
How can I make it that the thread will stop NICELY, but still finish it's
last epoch in the loop? Perhaps there is a better way to accomplish this?
Thank you, all.
Saul775