-
April 24th, 2000, 01:32 PM
#1
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 ?
-
April 24th, 2000, 03:44 PM
#2
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
-
April 24th, 2000, 07:39 PM
#3
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;
}
-
April 25th, 2000, 12:24 AM
#4
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
-
December 2nd, 2018, 07:02 AM
#5
Re: Messages are disappearing when using PostMessage
Could someone please give example with declaring how to put this code (ProcessMessageFilter) in C++ program?
-
December 2nd, 2018, 11:51 AM
#6
Re: Messages are disappearing when using PostMessage
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.
-
December 2nd, 2018, 12:46 PM
#7
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;
}
Last edited by 2kaud; December 2nd, 2018 at 01:21 PM.
-
December 2nd, 2018, 04:32 PM
#8
Re: Messages are disappearing when using PostMessage
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?
-
December 3rd, 2018, 05:19 PM
#9
Re: Messages are disappearing when using PostMessage
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?
-
December 3rd, 2018, 05:43 PM
#10
Re: Messages are disappearing when using PostMessage
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|