Discussion:
How does the main message loop handle idle?
(too old to reply)
Default User
2008-08-05 13:52:43 UTC
Permalink
Hi,

Ok, so if I create my own "mini" message loop like this:

while (waiting_on_something)
Application->ProcessMessages()

It spikes the CPU to 100%.

If I add Sleep(1), it will sleep for 15ms.

How does the main VCL messageloop handle idle time? Obviously it does it in
a way that doesn't consume all the CPU cycles.

Thanks,

Alan
Default User
2008-08-12 14:48:51 UTC
Permalink
Hi,

My previous message talks about the problem where I am trying to do
something in a loop, but doing this:

while (waiting_on_something)
Application->ProcessMessages()

Results in the best performance (no latency delays), but also pegs the CPU
at 100% which I find inacceptable.

Doing this:

while (waiting_on_something)
{
Application->ProcessMessages()
Sleep(1)
}

Resulted in no high CPU, but a serious performance hit if the code needed to
do somethiing quickly because even though you have only asked for a Sleep of
1 millisecond, it will end up being 15 milliseconds because of the way the
operating system handles it.

So, my solution is this:

while (waiting_on_something)
{
MsgWaitForMultipleObjectsEx(0,NULL,1,QS_ALLINPUT,0);
Application->ProcessMessages();
}

This will wait until there is something to process without consuming CPU
cycles, or timeout every 1 (actually becomes 15 milliseconds). If a message
comes in to process, it releases immediately without waiting out a 15
millisecond delay...

Thanks,

Alan

Loading...