Re: Too busy message loop?
Klaus Hartmann =E5=AF=AB=E9=81=93=EF=BC=9A
Correction: (I was typing too fast)
Note, that the above will still be very busy, when the application is
active.
"Klaus Hartmann" <niki_hartmann@t-online_add_dot_de> wrote in message
news:ePzMBf0kGHA.5108@TK2MSFTNGP02.phx.gbl...
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(). T=
his
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 v=
ery
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));
}
///////////////////////////////////////////////////////////////
Hi
Thank you very much.
My app will receive serial port data every ms, I think it is busy all
the time.
but I don't know the app occupy all resource... what's the suggestion
....
Best regards,
Boki.