Re: Too busy message loop?
What you are doing there is a typical message loop for a computer game. The
reason, why it is so busy is because you do not use GetMessage() in a loop.
If there's no message in the application's message queue, then GetMessage()
puts the thread to sleep till a message arrives (probably by calling
WaitMessage()). Since you don't call GetMessage(), the thread is never put
to sleep, and thus the loop executed all the time.
If this is for a game, then you're on the right path. You can, however,
enhance this, by having a flag that tells the message loop, if the
application is active. If it is not, then you can call WaitMessage(). This
way other Windows application will have a chance to run smoothly. (see
WM_ACTIVATE and WM_ACTIVATEAPP for finding out if you application is
active).
So a slighty enhanced version (for a game that doesn't need to continue
executing while it's not active - i.e., not an online game) would look like
this:
while (true)
{
if (!::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
if (!::GetMessage(&msg, NULL, 0, 0)) break;
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
else if (IsAppActive())
{
// Run your game logic here...
}
else
{
::WaitMessage();
}
}
Note, that the above will still be very busy, when the application is very
busy. If your message loop is for a normal application (i.e, not a game),
then do:
while (::GetMessage(&msg, NULL, 0, 0))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
"Boki" <bokiteam@ms21.hinet.net> wrote in message
news:1150682803.735470.304530@i40g2000cwc.googlegroups.com...
Hi All,
Here is my message loop, it seems very busy by the task monitor, how to
improve?
Best regards,
Boki.
///////////////////////////////////////////////////////////////////////////////////
...
PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE);
while (msg.message!=WM_QUIT) {
if (PeekMessage( &msg, NULL, 0, 0, PM_REMOVE)) {
// dispatch the message
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
// if (idle_load_jpg)
// LoadPictureFileI(UART_Read_Stream(4096,ImageBfr));
}
///////////////////////////////////////////////////////////////