PostMessage between two UI threads
Hi all,
I was kinda experimenting with PostMessage() between UI threads. I was able to post messages to the main thread of the dialog. But when I create a class derived from CWinThread, I am not able to register a function to receive the messages, in BEGIN_MESSAGE_MAP(). I am not able to compile. I get the following error.
error C2440: 'static_cast' : cannot convert from 'LRESULT (__thiscall UIThread::* )(WPARAM,LPARAM)' to 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)' Am I going the right direction? Please help.
Regards
[581 byte] By [
GN Raju] at [2007-11-20 1:28:30]

# 1 Re: PostMessage between two UI threads
Your function might have been defined as static or global. Perhaps you declared it outside of the class's definition, or omitted the class name, i.e.
LRESULT CMyWinThread::MyMessageHandler( WPARAM wParam, LPARAM lParam )
Mike
# 2 Re: PostMessage between two UI threads
Thanks Mike for the reply.
The function is neither static nor global. It is a member function of the class UIThread which is derived from CWinThread. The message map is something like this
BEGIN_MESSAGE_MAP(UIThread, CWinThread)
ON_MESSAGE(9, On9 )
END_MESSAGE_MAP()
Actually the class is created when I added an MFC class (in VS 2003). One doubt I have is how can there be a message map when the class is not derived from CWnd, or in other words, does not have window property?
Also I have a doubt in this line of the class declaration
public:
DECLARE_DYNCREATE(UIThread)
and in the implementation part (in the .cpp) there is
IMPLEMENT_DYNCREATE(UIThread, CWinThread)
Any idea what this means?
Thanks
# 3 Re: PostMessage between two UI threads
For your 2nd UIThread, you have to use CWinThread's PostThreadMessage or Win32's PostThreadMessage.
Hope it helps. :)
# 4 Re: PostMessage between two UI threads
OK. But that's the second issue. The first is, I am unable to compile. The error is in the message pump (BEGIN_MESSAGE_MAP) shown above.
Thanks.
# 5 Re: PostMessage between two UI threads
..The function is neither static nor global. It is a member function of the class UIThread which is derived from CWinThread.
Now I see; sorry.
For thread messages (as opposed to window messages), you must use the ON_THREAD_MESSAGE macro, not the ON_MESSAGE macro. See http://msdn2.microsoft.com/en-us/library/1cy89e70.aspx
Note that your use of "9" will cause problems. As far as I can tell from the windows.h file, "9" does not now correspond to a legal windows message. But still, it's better to define your own message ID's in the recommended way, which is something above WM_APP.
Also note that use of PostThreadMessage() is designed (yes, designed), to lose messages under very-common situations, such as when the user is moving/dragging a window, or when a modal dialog is being displayed, or any other situation where the thread is in a modal message loop. For this reason, you should not use thread messages or PostThreadMessage() for any thread that displays a window or a UI element. See http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/windowing/messagesandmessagequeues/messagesandmessagequeuesreference/messagesandmessagequeuesfunctions/postthreadmessage.asp
Mike
# 6 Re: PostMessage between two UI threads
Now I see; sorry.
For thread messages (as opposed to window messages), you must use the ON_THREAD_MESSAGE macro, not the ON_MESSAGE macro. See http://msdn2.microsoft.com/en-us/library/1cy89e70.aspx Ahh... now it works (atleast it compiles ;) ) . Thank you. :thumb:
Note that your use of "9" will cause problems. As far as I can tell from the windows.h file, "9" does not now correspond to a legal windows message. But still, it's better to define your own message ID's in the recommended way, which is something above WM_APP.Yea... but 9 is not supposed to be a legal windows message. It is our proprietary message. We used this in other of the working prototype.
Also note that use of PostThreadMessage() is designed (yes, designed), to lose messages under very-common situations, such as when the user is moving/dragging a window, or when a modal dialog is being displayed, or any other situation where the thread is in a modal message loop. For this reason, you should not use thread messages or PostThreadMessage() for any thread that displays a window or a UI element. See http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/windowing/messagesandmessagequeues/messagesandmessagequeuesreference/messagesandmessagequeuesfunctions/postthreadmessage.asp
MikeThanks for the info Mike. Caio :wave:
