Is there a way to use QueryPerformanceCounter & QueryPerformanceFrequency (as a delay/pause function) without skyrocketing CPU Usage?
Printable View
Is there a way to use QueryPerformanceCounter & QueryPerformanceFrequency (as a delay/pause function) without skyrocketing CPU Usage?
I think that's used only to measure time...
Why not do something like this ?
It won't make the CPU go high at all....VB Code:
Option Explicit Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Private Sub Form_Load() Pause 2 End Sub Private Sub Pause(Seconds As Single) Dim EndTime As Single EndTime = Timer + Seconds Do Until Timer > EndTime DoEvents Sleep 1 Loop End Sub
Yeah, but I need to use QueryPerformanceCounter for accuracy. Sleep/Timer/GetTickCout/everything else only "refreshes" 64 times a second. I need to be in the microseconds/milliseconds.
Is there a way to use QueryPerformanceCounter/Freq to set an interval in which code is executed?
This code here uses Sleep if necessary and makes sure the code is executed the required amount in seconds. You can actually make the code execute more often. It isn't perfectly accurate (thanks to Sleep), but a human can't possibly notice the millisecond differences. I can't really see anyone using this for thousands of iterations/second.
Attached a sample project which has one iteration (0) doing nothing for 1000 times/second, and another (1) for updating Form1.Caption every one seconds.
So basically this is just a very advanced version of the sample code by CVMichael (using QueryPerformanceCounter instead and controls the timings).
Awesome.
But, how would I prevent the program from going unresponsive?
If I make it do something once a second (Tick.Add 1) then It gets stuck every second (when I try to move the form around.).
Add another tick that does nothing; it is the downside of it, you must have one high frequency tick to keep things responsive. Don't worry, it only calls DoEvents when there is a need for it :)
30 - 50 ticks or so should be very much enough (to fool human to think the program is fully responsive). At only 5 - 10 ticks Windows starts to think the program is unresponsive.
Merri Christmas! :D