CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2000
    Posts
    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 ?



  2. #2
    Join Date
    Nov 1999
    Location
    Dresden / Germoney
    Posts
    1,402

    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




  3. #3
    Guest

    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;
    }




  4. #4
    Join Date
    Nov 1999
    Location
    Dresden / Germoney
    Posts
    1,402

    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


  5. #5
    Join Date
    Dec 2018
    Posts
    3

    Re: Messages are disappearing when using PostMessage

    Could someone please give example with declaring how to put this code (ProcessMessageFilter) in C++ program?

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Messages are disappearing when using PostMessage

    Quote Originally Posted by Serg7 View Post
    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.

  7. #7
    Join Date
    Dec 2018
    Posts
    3

    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.

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Messages are disappearing when using PostMessage

    Quote Originally Posted by Serg7 View Post
    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?

  9. #9
    Join Date
    Dec 2018
    Posts
    3

    Re: Messages are disappearing when using PostMessage

    Quote Originally Posted by Arjay View Post
    Is your project an MFC project?
    No, it's not.
    Does anyone have a solution for non-MFC project with disappearing messages of PostMessage?

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Messages are disappearing when using PostMessage

    Quote Originally Posted by Serg7 View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured