Messages are disappearing when using PostMessage
Hi
I have a problem with AfxGetApp()->PostThreadMessage:
I have a workerthread that wants to post it's result to the WinApp. My problem is that messages are lost when the WinApp shows a MessageBox.
Her is an example:
my WorkerThread does this in a loop:
static i = 0;
// The next line never asserts....:
VERIFY(AfxGetApp()->PostThreadMessage(WM_MYMESSAGEID, (WPARAM) iData, (LPARAM) i))
++i;
And my WinApp has the following messagehandler:
long CVIPApp::OnMyMessage(WPARAM data, LPARAM i)
{
TRACE("i = %d\n", (int) i);
MessageBox(0, "test", "Testing", MB_OK);
// Sleep(3000); // Sleep works fine
return 0
}
If the MessageBox is shown while the workerthread post a message, the message is lost. If however the WinApp sleeps when the message is beeing posted, the message is queued.
My quess is that the MessageBox receives my messages ???
Can anyone help ?
Re: Messages are disappearing when using PostMessage
Hi,
The problem is, the message box has 8as all modal dialogs) it's own message loop (simply executing GetMessage-DispatchMessage in a loop, plus some extra magic)
While modal dialog is executed, Messages to windows are dispatched perfectly well, but thread-only messages (with can only be handled from inside the message loop) will never pass through CWinApp's message loop, which is responsible for calling your OnMessage handler.
The nearest solution is to post the message either to the main window, or to some invisible "message only" window. (Windows messages will always be processed by the thread that created them, no matter what Send/Post/message you call)
Hope this helps you
Peter
Re: Messages are disappearing when using PostMessage
Another possible solution is to overwrite app's ProcessMessageFilter :
CMyApp::ProcessMessageFilter( int code, LPMSG msg)
{
if ( CWinApp::ProcessMessageFilter( code, msg ) )
return TRUE;
if ( msg->hwnd == NULL )
return PreTranslateMessage(msg); // CWinThread::PreTranslateMessage will dispatch the message
return FALSE;
}
Re: Messages are disappearing when using PostMessage
Cool Solution! And probably the easiest to implem,ent. I didn't expect that MFC by default hooks all messages.
a 'forever astonished what MFC does' Peter
Re: Messages are disappearing when using PostMessage
Could someone please give example with declaring how to put this code (ProcessMessageFilter) in C++ program?
Re: Messages are disappearing when using PostMessage
Quote:
Originally Posted by
Serg7
Could someone please give example with declaring how to put this code (ProcessMessageFilter) in C++ program?
The code above shows you how to do it in an MFC C++ program. If you aren't using MFC, then this solution isn't practical in a non-MFC program.
Re: Messages are disappearing when using PostMessage
I use Visual Studio 17 and C++. I have this often disappearing messages with PostMessage. Could someone please give example of this code with short *.cpp file in whole with "int APIENTRY wWinMain" section and headers? When I just add it, I got errors.
I mean this code:
Code:
CMyApp::ProcessMessageFilter( int code, LPMSG msg)
{
if ( CWinApp::ProcessMessageFilter( code, msg ) )
return TRUE;
if ( msg->hWnd == NULL )
return PreTranslateMessage(msg);
return FALSE;
}
Re: Messages are disappearing when using PostMessage
Quote:
Originally Posted by
Serg7
I use Visual Studio 17 and C++. I have this often disappearing messages with PostMessage. Could someone please give example of this code with short *.cpp file in whole with "int APIENTRY wWinMain" section and headers? When I just add it, I got errors.
I mean this code:
Code:
CMyApp::ProcessMessageFilter( int code, LPMSG msg)
{
if ( CWinApp::ProcessMessageFilter( code, msg ) )
return TRUE;
if ( msg->hWnd == NULL )
return PreTranslateMessage(msg);
return FALSE;
}
Is your project an MFC project?
Re: Messages are disappearing when using PostMessage
Quote:
Originally Posted by
Arjay
Is your project an MFC project?
No, it's not.
Does anyone have a solution for non-MFC project with disappearing messages of PostMessage?
Re: Messages are disappearing when using PostMessage
Quote:
Originally Posted by
Serg7
No, it's not.
Does anyone have a solution for non-MFC project with disappearing messages of PostMessage?
Have you debugged your app? Are you certain you are posting the messages to the hwnd of a wndproc where they should get handled?