   In a puzzle about the message loop
  I am sorry.English is not my first language,but i try my best to describe the problem.    After i hava read some book about game develop.I saw those codes in the game loop:   while(TRUE){ if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))   {    if (msg.message == WM_QUIT) break;   TranslateMessage(&msg; DispatchMessage(&msg;   }   }When i run the application,I found it toke a lot of CPU time.Why not we use the under codes instead of the above:   while(GetMessage(&msg,NULL,0,0)){  TranslateMessage(&msg; DispatchMessage(&msg;}Those codes would not make the application taking a lot of CPU.Instead almost zero   
  Usually, in the first variant, there is a call to an function to do a game frame when there's no messages to be processed, making the application update as soon as there's no input.loop until done {  if(peek message) {    handle message  }  else {    update game state  }} In the second variant, the loop blocks when there's no messages, and the game update function has to be invoked in some other way, like in response to input or by a timer.   ;  Thank you for helping me.    If i use the second codes,i can't do some thing such as an input or a timer when there is no message.So it uses the PeekMessage function to do different thing in if-else statement.    But how about the high CPU time which the application takes?It will always run until the msg.message==WM_QUIT.   ;  High CPU time isn't necessarily a bad thing. After all, if your game is active and the user actually playing it, then it should take close to 100% of the CPU time. If it isn't then that means it's spending time doing other things, which isn't what the user's interested in right now.What most games (should) do is to use both loops - the PeekMessage based loop while the game is active, and then the GetMessage based loop while the game is paused or minimized (i.e. the user is concentrating on something else). That way it runs as fast as possible while it's actually active, but falls back to a low CPU use when you need to work with another program for a minute.   ;  Users will complain about 100% cpu even for games.  And the "it doesn't matter for games" argument doesn't apply to casual games and other small-scale stuff where the user might well be multi-tasking (e.g. I get a lot of minesweeper played while opening slow websites).The quick fix is to change your loop to:if (PeekMessage(...)){...}else{   Sleep(1);}The more advanced solution would use MsgWaitForMultipleObjects and possibly adjusting the sleep time based on the current frame rate.   