Results 1 to 5 of 5

Thread: [RESOLVED] wait function

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    528

    Resolved [RESOLVED] wait function

    if i use this wait function in my program:

    Code:
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    
    Public Sub Wait(Time As Long)
      Dim StartTime As Long
      
      StartTime = GetTickCount()
      
      While (StartTime + Time) > GetTickCount()
        DoEvents
      Wend
    End Sub
    i find that there is no difference between
    wait(10)
    and
    wait(1)

    i need my program to do this because i have a loop which i want to run at the same speed even on slow comps so i have programmed it so that it times how long it takes for the comp to complete the instructions in one instance of the loop and then it waits for 10 milliseconds minus the time it took to complete the instructions, for example if the instructions took 4 milliseconds, it would wait 6 milliseconds before looping round again, so the loop always takes 10 milliseconds. (except on rediculously slow comps where the instructions take more than 10 milliseconds, which isnt likely) but this isnt working because wait(10) and wait(1), wait for the same amount of time!
    how can i get a more accurate wait function?

  2. #2
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: wait function

    Both the native vb Timer function and the GetTickcount API function have a limited resolution of 0.015625 (1/64) seconds i.e. they can only measure time in multiples of that resolution. QueryPerformanceCounter combined with QueryPerformanceFrequency can be used to measure very small increments of time.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    528

    Re: wait function

    sorry I dont know what QueryPerformanceCounter and QueryPerformanceFrequency are, what are they and how would i use them to measure a small amount of time?

  4. #4
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: wait function

    vb Code:
    1. Option Explicit
    2. Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
    3. Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long
    4. Dim cTFreq As Currency
    5. Dim cTStart As Currency
    6. Dim cTEnd As Currency
    7.  
    8.  
    9. Private Sub Form_Load()
    10.   Dim T As Single
    11.   QueryPerformanceFrequency cTFreq 'Generally the frequency of your processor
    12.  
    13.   QueryPerformanceCounter cTStart 'Generally the number of cycles of your processor
    14.   Wait 0.001
    15.   QueryPerformanceCounter cTEnd
    16.   T = (cTEnd - cTStart) / cTFreq  'cycles / frequency = time
    17.   Debug.Print "And that took " & Round(T, 4) & " seconds"
    18. End Sub
    19.  
    20. Public Sub Wait(ByVal sSeconds As Single)
    21.   Dim T1 As Currency, T2 As Currency
    22.   QueryPerformanceCounter T1
    23.   T1 = T1 + sSeconds * cTFreq
    24.   Do
    25.     DoEvents 'Comment out for more accuracy, leave in if waiting for long periods
    26.     QueryPerformanceCounter T2
    27.   Loop Until T2 => T1
    28. End Sub
    Last edited by Milk; Jun 14th, 2007 at 07:49 AM.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    528

    Re: wait function

    thanks alot, thats just what i needed

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