simple program hogs the processor

BlitzPlus Forums/BlitzPlus Programming/simple program hogs the processor

julianbury(Posted 2004) [#1]
I noticed that this very simple program hogs the processor as view in the task manager.
Other people's sofware, like Winamp, does not - even when it's busy.
Is this a bug?
Run this tiny program and evoke the task manager to see what I mean.
Graphics 200,100,0,2
SetBuffer BackBuffer()

Repeat
Until KeyHit(1)
End


I compiled it to see if that would make any deference. It did not.


soja(Posted 2004) [#2]
You're in what's called a "busy loop". You've got the CPU executing instructions (repeat until) as fast as it can. You'll find that any program like this will do that.

One of the advantages of BlitzPlus (as opposed to Blitz3D and BlitzBasic) is that it has nice event-based capabilities. This means that instead of always polling for something (like keyhit), it can just sit back and wait for messages from Windows to tell it when to do something.

Instead of the repeat until keyhit, try changing it to something like
While WaitEvent() <> $101 : Wend
and see what happens.


Clyde(Posted 2004) [#3]
What does $101 represent please soja?


joncom2000(Posted 2004) [#4]
$101 - Key down
Generated when the user presses a key. EventData contains the 'raw scancode' of the key.

It's part of B+ GUI/Windows commands, listed in the manual ;)


BlitzSupport(Posted 2004) [#5]
And if you don't want to go event-based, a simple 'Delay 5' (or almost any code at all) will stop such a loop locking things up...


Eikon(Posted 2004) [#6]
Why not Delay 1?


julianbury(Posted 2004) [#7]
Thanks for that, Soja :-)

That clarifies things a lot.


xlsior(Posted 2004) [#8]
Why not Delay 1?


You could, but delay 5 will save you more CPU cycles... As far as a user is concerned, 5 milliseconds is instantaneous anyway, so checking each 5 milliseconds for a certain condition would look exactly the same as checking every 1 milliseconds... Except the longer delay keeps the PCU utilization lower.


burdall(Posted 2004) [#9]
Thanks to all -- I was having exactly the same problems with a loop!