Re: How to PostThreadMessages?
On Tue, 5 Jun 2007 13:18:06 -0500, "jp2code" <poojo.com/mail> wrote:
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
Don't base your messages on WM_USER. Use WM_APP or RegisterWindowMessage
instead. For more, see:
Which message numbers belong to whom?
http://blogs.msdn.com/oldnewthing/archive/2003/12/02/55914.aspx
Also, if you're going to use a macro, you must use parens:
#define MY_THREADMSG (WM_APP+1)
This may not be all that important for a message identifier, but it is
necessary to prevent the macro expansion from combining incorrectly with
operators in expressions that use the macro. It doesn't apply here, but
consider the difference the parens make in X/2, where X is your macro. All
that said, the preferred approach in C++ is to use a const object:
const int MY_THREADMSG = WM_APP+1;
You don't need parens for that.
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;
}
}
Don't use AfxEndThread. You won't execute any subsequent code, such as the
next statement, which sets g_LoopSerial to FALSE, nor will destructors run
for local variables, since the stack isn't unwound. For an UI thread, you
should simply call PostQuitMessage. Also, since ZSerialThread is derived
from CWinThread, and OnMessage isn't static, you don't need to use
AfxGetThread; you can refer to the unadorned m_nThreadID, which you
inherited from CWinThread.
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!
You're starting the thread suspended and resuming it from within
InitInstance, which runs in the context of the thread. This is pointless,
because suspended threads don't run. You can resume a thread X only from
another thread Y. Also, you're relying on the (default) auto-delete
behavior, which is a race condition waiting to happen. See my web page for
more:
http://members.cox.net/doug_web/threads.htm
--
Doug Harrison
Visual C++ MVP