100%cpu usage with PeekMessage

This is a discussion on 100%cpu usage with PeekMessage within the Windows Programming forums, part of the Platform Specific Boards category; I've just noticed something, if I use this piece of code for processing messages, I get 100% CPU usage when ...

  1. #1
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345

    100%cpu usage with PeekMessage

    I've just noticed something, if I use this piece of code for processing messages, I get 100% CPU usage when the window is in the background:
    Code:
    done = false;						
    while (!done) {
        PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);
        if (msg.message == WM_QUIT) {
            done = true;
        } else {
            TranslateMessage(&msg);
    	DispatchMessage(&msg);
        }
    }
    while, this doesn't cause 100% CPU usage:
    Code:
    while (GetMessage(&msg, NULL, 0, 0)) {
    	TranslateMessage(&msg);
    	DispatchMessage(&msg);
    }
    Any idea why?

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    GetMessage is blocking. PeekMessage( ) isn't.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    then I wonder why NeHe uses PeekMessage instead of GetMessage, his basecode cause 100% CPU usage when inactive, is there a reason for using PeekMessage or I should email him suggesting to change it to GetMessage?

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    GetMessage() will not return until there is a msg. Therefore no processing outside the main loop is possible.

    PeekMessage() just asks for a msg and returns (even if there is no msg). This allows other processing to be done while the app would normally be idle (waiting for the GetMessage() to return with a msg).

    Use Peek only for games ect not normal apps.





    EDIT Spelling:: Pity there is not a WIN32 function 'GetMassage()'
    Last edited by novacain; 08-19-2003 at 04:02 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    If you need to use PeekMessage, place a WaitMessage prior to the PeekMessage call.

  6. #6
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    Originally posted by novacain
    GetMessage() will not return until there is a msg. Therefore no processing outside the main loop is possible.

    PeekMessage() just asks for a msg and returns (even if there is no msg). This allows other processing to be done while the app would normally be idle (waiting for the GetMessage() to return with a msg).

    Use Peek only for games ect not normal apps.





    EDIT Spelling:: Pity there is not a WIN32 function 'GetMassage()'
    I figured this out later, thx, so this the reason why NeHe uses PeekMessage, otherwise, if there are no messages, the screen won't be updated.

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    a microsoft API called GetMassage? who would do it? Gates? ewwww.....
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reduce CPU usage
    By patrick22 in forum Windows Programming
    Replies: 9
    Last Post: 07-10-2009, 02:13 PM
  2. Net cpu usage of pthreads?!
    By mynickmynick in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2008, 07:59 AM
  3. Calculating CPU Usage
    By vitaliy in forum Linux Programming
    Replies: 3
    Last Post: 08-21-2005, 09:38 AM
  4. PeekMessage function
    By Micko in forum Windows Programming
    Replies: 9
    Last Post: 09-14-2004, 03:06 PM
  5. Win2K Limiting CPU Usage?
    By drdroid in forum Tech Board
    Replies: 4
    Last Post: 03-31-2004, 01:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21