Yeah, while I can't think of a good reason to poll inkey incessantly, it also seems wrong to make this decision for the programmer.
I am still tempted to do it in lang qb though. In QBASIC, programs were written for single-tasking environments, and sub-second Sleeping wasn't available. The programs would also run a lot more slowly anyway.
Why is CPU usage near 100%?
-
- Site Admin
- Posts: 6323
- Joined: Jul 05, 2005 17:32
- Location: Manchester, Lancs
-
- Site Admin
- Posts: 6323
- Joined: Jul 05, 2005 17:32
- Location: Manchester, Lancs
Re: Why is CPU usage near 100%?
Hmm. I was thinking about crippling it in lang qb so it couldn't be polled more times per second than it does in QBASIC.
But then I did a speed test (FB Win32 vs QB NTVDM), and I found that an Inkey loop actually runs about ten times faster in QB than in FB!
But then I did a speed test (FB Win32 vs QB NTVDM), and I found that an Inkey loop actually runs about ten times faster in QB than in FB!
Re: Why is CPU usage near 100%?
I got same problem about the CPU Usage when use Inkey and the only solution for that is the sleep and timer like in this source code
Thank you Dkl !
Its another solution or This work fine ?
Thank you Dkl !
Code: Select all
Dim Ciclic as Integer
Dim Button as String
Dim Wt As Double
Print "This is a test IDLE for find the usage of CPU when use a Loop with Inkey function"
Print "Step 1 : Inkey with Loop without control"
Ciclic = 0
Print "Pres a Key"
Do While Ciclic = 0
Button = Inkey
If len(Button) > 0 Then Ciclic = 1
Loop
Print "Key pressed = " & Button
Ciclic = 0
Print "Step 2 : Inkey with Sleep control"
Print "That make slower the cicle of loop and the the CPU Usage"
Print "Pres a key"
Do While Ciclic = 0
Button = Inkey
If len(Button) > 0 Then
Ciclic = 1
Else
sleep 25
End If
loop
Print "Key pressed = " & Button
Ciclic = 0
Print "Step 3 : Inkey with Sleep and Timer control"
Print "You are able to use the sleep function after a determinated period"
Print "That lower the power consume if you are away from keyboard or can start another routine"
Print "Pres a key"
WT = Timer
Do While Ciclic = 0
Button = Inkey
If len(button) > 0 Then
Ciclic = 1
Else
If (Timer - WT) > 5 Then
Sleep 25
End If
If (Timer - WT) > 59 Then
Sleep 250
End If
If (Timer - WT) > 120 Then
Sleep 500
End If
If (Timer - WT) > 180 Then
Print "End program after 180 Seconds"
End
End If
End If
Loop
Print "Key pressed = " & Button
Print "Test end"
End
Re: Why is CPU usage near 100%?
A simple sleep 1 reduces the inkey polling from ~1 million times a second to less then 1000 times a second. Which means that the cpu is then doing > 99.9% of time nothing (or something else).