Older Version
Newer Version
Alyce
Jun 17, 2011
=MINIMIZE CPU USAGE IN SCAN LOOP=
[[user:DennisMcK]]
It recently came to my attention that some LB programmers were avoiding scan loops because they use 100% of the processor. Scan loops are necessary if you use WM_Liberty.dll or almost anything else that uses LB callbacks. Although the 100% usage is true there's a simple way to cure this. An api call to Sleep is all that's needed. The following two programs were tested on a PII 400 that had 27 tasks running in the background along with Liberty BASIC. As the pictures show the program without the Sleep call used 100% of the processor while the program with the sleep call used only 1%. That 1% included all of the other running tasks too.
[[image:100.gif]]
[[code format="lb"]]
'-------- 100%
nomainwin
open "Use all of that cpu" for window as #1
#1 "trapclose [quit]"
[loop]
scan
goto [loop]
[quit]
close #1: end
[[code]]
[[image:1.gif]]
[[code format="lb"]]
'-------- 1%
nomainwin
open "Minimun cpu uasage" for window as #1
#1 "trapclose [quit]"
[loop]
scan
'sleep for 50 milliseconds
calldll #kernel32,"Sleep",50 as ulong,r as void
goto [loop]
[quit]
close #1: end
[[code]]