CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: on events

  1. #1
    Join Date
    Mar 2018
    Posts
    11

    on events

    what is the difference between (i) and (ii):

    (i)
    void DoEvents()
    {
    MSG msg;
    while(::PeekMessage(&msg, NULL, NULL, NULL, PM_NOREMOVE))
    {
    if (!AfxGetApp()->PumpMessage())
    return;
    }
    }
    (ii)
    void DoEvents()
    {
    MSG msg;
    while (::GetMessage(&msg, NULL, NULL, NULL))
    {
    if (!PreTranslateMessage(&msg))
    {
    ::TranslateMessage(&msg);
    :ispatchMessage(&msg);
    }
    }
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: on events

    Well, trying to read/understand the basics from MSDN would be a good start!
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2018
    Posts
    11

    Re: on events

    thanks for the link, however, I needed more information on AfxGetApp()->PumpMessage(). I got more understanding about it by looking at some example codes and the code comments. MSDN does not seem to have much info on AfxGetApp()->PumpMessage().??
    Thanks.

  4. #4
    Join Date
    Aug 2006
    Posts
    231

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: on events

    Completing what already is answered above:

    CWinThread::PumpMessage is documented in MSDN. Note that AfxGetApp gets a pointer to the application object which is of a class derived from CWinApp and CWinApp is derived from CWinThread.

    Generally, you don't need to directly call PumpMessage in your MFC application.

    If you want to see how CWinThread::PumpMessage is implemented, you can have a look in MFC framework source code.
    Search it in <Visual Studio install folder>\VC\atlmfc\src\mfc or run your application in DEBUG mode then step into. See also Visual Studio 2015: How to Step into MFC Framework Code.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: on events

    Quote Originally Posted by Y$EA View Post
    what is the difference between (i) and (ii):
    The main difference is between PeekMessage(PM_NOREMOVE) and GetMessage(). PeekMessage() just peeks into message queue and lets you quit when the queue is empty. GetMessage() does not return until a message appears in the queue, so you have no chance to quit the DoEvents() function until the very moment of application shutdown.

    Making long story short: GetMessage() blocks the thread, and PeekMessage() does not. Using DoEvents() based on PeekMessage() you may interleave your worker thread processing with pumping message queue. Otherwise you have to run your non-GUI stuff (typically something to do with heavy calculation or blocking communication) in a separate thread.

    To understand the picture in whole you need to read MSDN regarding all the functions mentioned in the two snippets.
    Last edited by Igor Vartanov; April 3rd, 2018 at 12:03 PM.
    Best regards,
    Igor

Tags for this Thread

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