Click to See Complete Forum and Search --> : Send message from worker thread to main thread


retry
December 22nd, 2004, 02:52 PM
I have posting this as a new post since the last one ended up with this:

I am posting message from worker to main thread but I have ran into problem that the message is never recieved in the main application thread.

In the App class, I have added the following code to message map to catch the message.

ON_THREAD_MESSAGE(IDM_USER_MSG1, OnUserMsg1)

and than OnUserMsg1 is defned as:

LRESULT OnUserMsg1(WPARAM wParam, LPARAM lParam)
{
.....
return 0;
}

The PostThreadMessage() in the worker thread does return 1 which means it is posted properly. What am I missing in catching this message? Thanks.

I am posting the message with this call:
AfxGetApp()->PostThreadMessage(IDM_USER_MSG1, ... );

I have tried ON_MESSAGE macro as well but that doesn't catch the message either. In addition I have peeked into PreTranslateMessage() but I cann't that message either.

BOOL CHelloApp::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if (pMsg->hwnd == NULL)
{
if (pMsg->message == IDM_USER_MSG1)
AfxMessageBox("Got it"); // sorry no such message here
}
return CWinApp::PreTranslateMessage(pMsg);
}

Paul Rice
December 22nd, 2004, 03:06 PM
Why not pass the window handle to the thread and use PostMessage() (SendMessage()?)?

OReubens
December 22nd, 2004, 03:12 PM
What value is IDM_USER_MSG1 ?
It should be >= WM_APP
typically, you would define these something like

#define IDM_USER_MSG1 (WM_APP+1)
#define IDM_USER_MSG2 (WM_APP+2)
#define IDM_USER_MSG3 (WM_APP+3)

What version of VC/MFC are you using ?

OReubens
December 22nd, 2004, 03:15 PM
Why not pass the window handle to the thread and use PostMessage() (SendMessage()?)?

While that may sometimes be a solution, it isn't always one. A thread may have a messageloop but NOT have any windows.
Beside, maybe the message must be handled in the application, and not in the frame or one of the other windows owned by the thread.

retry
December 22nd, 2004, 03:31 PM
I was using WM_USER + 1, however I changed that to WM_APP + 1 now but still not recieving the message.

In my case although I am sending the message to main thread (so has GUI), I am not sure if SendMessage or PostMessage() should work across the threads? Another follow up is that can we handle the thread message anywhere in the application, like for example in a view in MDI?

I am using VC++ 6 with all updates there is to it on XP Pro.

OReubens
December 22nd, 2004, 05:07 PM
Have you checked this out ?
http://support.microsoft.com/default.aspx?scid=kb;en-us;183116

In VC6 there are a couple of problems related to ON_THREAD_MESSAGE. While Windows itself properly handles the message, it gets lost somewhere in the inner workings of MFC. You may have to use PostThreadMessage ONLY on non-GUI threads, and posting to a regular window (the mainframe is the ideal candidate) for the main GUI thread.

Part of the problem could also be that you have the wrong kind of prototype for ON_THREAD_MESSAGE. The correct prototype returns void, not LRESULT.

As far as I can tell, it works correctly on VC7. but I can't (at this moment) test for VC6.

May I suggest switching to VC7. It's a free download anyway...
http://msdn.microsoft.com/visualc/vctoolkit2003/

retry
December 22nd, 2004, 05:53 PM
Thanks I think that is the problem. While running application, I did get the notification only couple of times but looks like they are lost most of the time. :thumb:

Andreas Masur
December 23rd, 2004, 03:50 AM
[ Moved thread ]

Andreas Masur
December 23rd, 2004, 03:52 AM
If you would have looked at the FAQs, you would have found this (http://www.codeguru.com/forum/showthread.php?t=312454)...which shows how to send a message from a worker thread to the main thread