Results 1 to 7 of 7

Thread: QueryPerformanceCounter (CPU Usage)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    cali
    Posts
    243

    QueryPerformanceCounter (CPU Usage)

    Is there a way to use QueryPerformanceCounter & QueryPerformanceFrequency (as a delay/pause function) without skyrocketing CPU Usage?
    Last edited by ICESTORM; Dec 24th, 2006 at 02:06 AM.
    ......

  2. #2
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: QueryPerformanceCounter (CPU Usage)

    I think that's used only to measure time...

    Why not do something like this ?
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    4.  
    5. Private Sub Form_Load()
    6.     Pause 2
    7. End Sub
    8.  
    9. Private Sub Pause(Seconds As Single)
    10.     Dim EndTime As Single
    11.    
    12.     EndTime = Timer + Seconds
    13.    
    14.     Do Until Timer > EndTime
    15.         DoEvents
    16.         Sleep 1
    17.     Loop
    18. End Sub
    It won't make the CPU go high at all....

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    cali
    Posts
    243

    Re: QueryPerformanceCounter (CPU Usage)

    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.
    ......

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    cali
    Posts
    243

    Re: QueryPerformanceCounter (CPU Usage)

    Is there a way to use QueryPerformanceCounter/Freq to set an interval in which code is executed?
    ......

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: QueryPerformanceCounter (CPU Usage)

    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).
    Attached Files Attached Files
    Last edited by Merri; Dec 23rd, 2006 at 06:40 AM.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    cali
    Posts
    243

    Re: QueryPerformanceCounter (CPU Usage)

    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.).
    ......

  7. #7
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: QueryPerformanceCounter (CPU Usage)

    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!
    Last edited by Merri; Dec 24th, 2006 at 08:16 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width