-
How to create delay in program witch will be same length on every computer (from 100 MHz 486 to Pentium III). I do some computing in in this time interval and timer on the slow machine seems to slow down.
I need exact delay 700 ms (+ - 50 ms).
What can I do?
Ermin
-
Just place a timer control and your code in the events of the timer
:)
-
I already put timer in my program. But problem is next:
With some event (click) I start part of the program that does his job. In the same time I start timer (700 ms) and timer dramatically slows down and it’s time period is useless.
Thnx
-
on a slow machine. On mine works fine.
-
Here is some code that I have that creates an extremely accurate timer down to 1ms resolution.
Put this at the top of the code:
Dim Time as HighResTimer
Set Time = New HighResTimer
Wherever you want your delay, just put the following code:
Time.EnterBlock
While Time.ElapsedTime <= 700
DoEvents
Time.ExitBlock
Wend
Put this code into a class module called HighResTimer:
'The number is codified as HighPart*2^32+LowPart
Private Type LARGE_INTEGER
LowPart As Long
HighPart As Long
End Type
Private Declare Function QueryPerformanceCounter Lib _
"kernel32" (lpPerformanceCount As LARGE_INTEGER) _
As Long
Private Declare Function QueryPerformanceFrequency Lib _
"kernel32" (lpFrequency As LARGE_INTEGER) As Long
Private m_TicksPerSecond As Double
Private m_LI0 As LARGE_INTEGER
Private m_LI1 As LARGE_INTEGER
Public Sub Class_Initialize()
Dim LI As LARGE_INTEGER
If QueryPerformanceFrequency(LI) <> 0 Then
m_TicksPerSecond = LI2Double(LI)
Else
m_TicksPerSecond = -1
End If
End Sub
Public Property Get Resolution() As Double
Resolution = 1# / m_TicksPerSecond
End Property
Public Sub EnterBlock()
QueryPerformanceCounter m_LI0
End Sub
Public Sub ExitBlock()
QueryPerformanceCounter m_LI1
End Sub
Public Property Get ElapsedTime() As Double
Dim EnterTime As Double, ExitTime As Double
EnterTime = LI2Double(m_LI0) / m_TicksPerSecond
ExitTime = LI2Double(m_LI1) / m_TicksPerSecond
ElapsedTime = ExitTime - EnterTime
End Property
Private Function LI2Double(LI As LARGE_INTEGER) As Double
Dim Low As Double
Const TWO_32 = 4# * 1024# * 1024# * 1024#
Low = LI.LowPart
If Low < 0 Then Low = Low + TWO_32
'Now Low is in the range 0...2^32-1
LI2Double = LI.HighPart * TWO_32 + Low
End Function
Let me know if you need more help
-
This one is more simple, and works fine:
Tmr = Timer + 0.7
Do Until Timer > Tmr
DoEvents
Loop
-
The only problem I see with that kedaman is that it will not be consistent. It will be all over from 700 ms to 760 ms and maybe more depending on the speed of the computer. The greatest resolution timer you can hope to get is 53 ms using any sort of tick counter. And the slower the computer the worse the resolution.
-
Well, don't bother anymore. Code from
glotzbam
works fine. Many thanks.
I try it on a very slow computer and it worked.
Thanks again. U saved me a lot of time.
Ermin
-
"Well, don't bother anymore"
I still bother, cause I don't like that people telling me to get lost. I can see why you glotzbam, tells me it would take 700-760ms on slow computers. However it is because the code that takes the performance (60ms) is put after the timer.
If you put the code between these lines:
Tmr = Timer + 0.7
Do Until Timer > Tmr
It will always count 700ms, unless the code itself will run in more than 700ms.
-
Hmm. I newer tried to tell you to get lost. Code works for me and that's all that matters.
Thnx!
-
I never tried to tell you to get lost either, I was just simply trying to provide the best way. I think your way is the best way of doing it most of the time when you don't need a critical timer. Sorry if I upset you, but there is no need to blow up like that.
-
I just wanted to clear up using timers. I have researched this alot. I have had scopes hooked up to my computer implementing several different types. The use of any windows timer references the bios tick counter. That is at a resolution of 53ms. Therefore you are guarenteed to be +-53ms off. Like I said I have had scopes hooked up, it is inaccurate, and unprecise.
-
I was refering to kedaman. But in a joke manner.
Anyway thanks to everyone that was or will be replaying to my subject.
Maybe we could share out knowledge in some bar with beer cans all over the place and worked out our different approach.
Have a good time!
-
Sorry, Im from Finland and my English is not quite good, It's influenced from computerlanguage and other things, so it's sometimes hard to express myself. :) But, that's not what I was to post here.
Thanx glotzbam for your code, I found it very useful! My timers really didn't pass the test