How to manage user messege sent by PostThreadMessage
Hi - I have problem with receiving a message sent by PostThreadMessage. I'm posting message from main application class (CWinApp). I'm posting it to my class derived from CWinThread. Part of my thread class code:
CReaderThread.h
I set breakpoints after PeekMessage(), in OnMessageTest() and in PreTranslateMessage() - nothing. Messages are lost. How to make my thread to receive sended messages?
Greetings.
AragornX
Last edited by aragornx; April 27th, 2010 at 04:23 AM.
Reason: errors
Re: How to manage user messege sent by PostThreadMessage
about AfxEndThread - http://msdn.microsoft.com/en-us/libr...8VS.80%29.aspx
"Must be called from within the thread to be terminated." - so I used it.
and about message loop - do I need to write it by myself ? I thought that it's implemented in CWinThread. Could you write me a sample of that message loop, and where should I place it ? I need to execute some code in same time intervals - I doing it in my mainLoop()... Thank you for answers...
Re: How to manage user messege sent by PostThreadMessage
I do not remember already how to do that in MFC, but in WinAPI one must do something along the lines of:
BOOL rv;
MSG msg = {};
while (0 != (rv = GetMessage(&msg, 0, 0, 0)))
{
if (-1 == rv)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Re: How to manage user messege sent by PostThreadMessage
Originally Posted by dvyukov
AfxEndThread() call looks a bit suspicious. What it's for?
I agree. It is wrong to call AfxEndThread() here. Just return TRUE from InitInstance() is enough and correct.
Originally Posted by dvyukov
Also I do not see where you start message loop, where you pump messages.
It is started within CWinThread class (CWinThread::Run method).
So there is no need to explicitely call GetMessage nor TranslateMessage/DispatchMessage
Last edited by VictorN; April 27th, 2010 at 06:18 AM.
Reason: Fixed my own mistake
The code in bold text is wrong. It is always wrong to do something extensive in InitInstance(). The purpose of InitiInstance) is initialization only, after which you should simply return TRUE so as to allow the built-in message loop to commence. Your code, on the other hand, traps the InitiInstance() function, by calling your mainLoop() function forever, and never allows InitInstance() to return. Thus, you block the built-in message loop from ever even starting.
Re: How to manage user messege sent by PostThreadMessage
Incidentally, it is always a bad architecture to use PostThreadMessage for posting messages to a thread that has any form of a visible window. If the thread hosts any form of a visible window, then the messages posted to it will inevitably be lost under very common circumstances.
Bookmarks