Re: How to PostThreadMessages?
jp2code wrote:
Mr. McPhillips, your post was the clearest to understand, except for the
part where you said, "your thread's InitInstance will execute, and then the
thread goes to sleep until it receives a message." Is there any specific
message I need to give my thread to begin execution? I would be happy if the
thread started running my scanning loop as soon as it were created.
I have also created my Thread Message loop as follows:
//---------------------------------------------------------------------------
void ZSerialThread::OnSerialMessage(WPARAM wParam, LPARAM lParam)
{
switch (wParam)
{
case NEWCOM: // change COM Port
if (lParam != NULL)
ChangeCommPort((LPTSTR)lParam);
break;
case STARTIT: // Start the Loop!
ScanSerialPort();
break;
case TH_DON: // End the thread
PostQuitMessage(0);
g_LoopSerial = FALSE;
break;
}
}
Any comments, good or bad?
You have not made clear how long your "scanning loop" runs. As long as
that loop is running your message handler will not be called. A
message-driven thread should be structured to process a message then
return to the MFC message loop. If you don't do that you won't get
messages. Do you want your thread looping forever in the scanning loop,
or do you want it waiting for messages?
--
Scott McPhillips [MVP VC++]