|
-
Jun 14th, 2007, 05:48 AM
#1
Thread Starter
Fanatic Member
[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?
-
Jun 14th, 2007, 06:31 AM
#2
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.
-
Jun 14th, 2007, 07:05 AM
#3
Thread Starter
Fanatic Member
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?
-
Jun 14th, 2007, 07:39 AM
#4
Re: wait function
vb Code:
Option Explicit
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long
Dim cTFreq As Currency
Dim cTStart As Currency
Dim cTEnd As Currency
Private Sub Form_Load()
Dim T As Single
QueryPerformanceFrequency cTFreq 'Generally the frequency of your processor
QueryPerformanceCounter cTStart 'Generally the number of cycles of your processor
Wait 0.001
QueryPerformanceCounter cTEnd
T = (cTEnd - cTStart) / cTFreq 'cycles / frequency = time
Debug.Print "And that took " & Round(T, 4) & " seconds"
End Sub
Public Sub Wait(ByVal sSeconds As Single)
Dim T1 As Currency, T2 As Currency
QueryPerformanceCounter T1
T1 = T1 + sSeconds * cTFreq
Do
DoEvents 'Comment out for more accuracy, leave in if waiting for long periods
QueryPerformanceCounter T2
Loop Until T2 => T1
End Sub
Last edited by Milk; Jun 14th, 2007 at 07:49 AM.
-
Jun 14th, 2007, 10:31 AM
#5
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|