simple program hogs the processor
BlitzPlus Forums/BlitzPlus Programming/simple program hogs the processor
| ||
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. |
| ||
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 : Wendand see what happens. |
| ||
What does $101 represent please soja? |
| ||
$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 ;) |
| ||
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... |
| ||
Why not Delay 1? |
| ||
Thanks for that, Soja :-) That clarifies things a lot. |
| ||
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. |
| ||
Thanks to all -- I was having exactly the same problems with a loop! |