Results 1 to 14 of 14

Thread: Exact delay in VB program

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Location
    SLOVENIA, Europe
    Posts
    110
    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

  2. #2
    Junior Member
    Join Date
    Apr 1999
    Location
    Location Greece.
    Posts
    25
    Just place a timer control and your code in the events of the timer

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Location
    SLOVENIA, Europe
    Posts
    110
    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

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Location
    SLOVENIA, Europe
    Posts
    110
    on a slow machine. On mine works fine.

  5. #5
    Member
    Join Date
    Mar 2000
    Posts
    32
    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

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    This one is more simple, and works fine:

    Tmr = Timer + 0.7
    Do Until Timer > Tmr
    DoEvents
    Loop

  7. #7
    Member
    Join Date
    Mar 2000
    Posts
    32
    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.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Location
    SLOVENIA, Europe
    Posts
    110
    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

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    "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.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Location
    SLOVENIA, Europe
    Posts
    110
    Hmm. I newer tried to tell you to get lost. Code works for me and that's all that matters.

    Thnx!

  11. #11
    Member
    Join Date
    Mar 2000
    Posts
    32
    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.

  12. #12
    Member
    Join Date
    Mar 2000
    Posts
    32
    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.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Location
    SLOVENIA, Europe
    Posts
    110
    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!

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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


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