Re: How to PostThreadMessages?
PostThreadMessage does not seem to work in my CWinThread inherited class.
Here is what I am doing:
1. Define my message:
#define MY_THREADMSG WM_USER + 1
2. Declare my message handler:
ON_THREAD_MESSAGE(MY_THREADMSG, OnMessage)
3. Create a thread:
g_pSerial = (ZSerialThread*)AfxBeginThread(RUNTIME_CLASS(ZSerialThread),
THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL);
4. Send messages to my thread:
g_pSerial->PostThreadMessage(MY_THREADMSG, (WPARAM)val1, (LPARAM)val2);
But my OnMessage function never gets called:
void ZSerialThread::OnMessage(WPARAM wParam, LPARAM lParam)
{
switch (wParam) {
case NEWCOM:
if (lParam != NULL)
ChangeCommPort((LPTSTR)lParam);
else
CloseSerialPort();
break;
case SCANIT:
ScanSerialPort();
break;
case SETBOOT:
SetBootModePin((BOOL)lParam);
break;
case STARTIT:
StartSerialPort((LPTSTR)lParam);
break;
case TH_DON:
PostQuitMessage(0);
AfxEndThread(AfxGetThread()->m_nThreadID, TRUE);
g_LoopSerial = FALSE;
break;
}
}
Could there be something wrong with the way I implimented the InitInstance
(below)?
BOOL ZSerialThread::InitInstance()
{
// TODO: Add your specialized code here and/or call the base class
configID = NULL;
g_hPort = INVALID_HANDLE_VALUE;
m_connecting = FALSE;
m_bAutoDelete = TRUE;
CWinThread::ResumeThread();
return CWinThread::InitInstance();
}
//---------------------------------------------------------------------------
Yes, I am a little confused about this. I'll take any help I can get!
Regards,
Joe