Waiting for less than one second

Discussion in 'Code Snippets and Tutorials' started by Billy, Apr 14, 2007.

  1. Billy

    Billy Level IV

    Joined:
    Feb 21, 2007
    Messages:
    1,856
    Likes Received:
    39
    how do you wait for less than one second in vb? i tried using secondstowait(number) but it rounds up to one second or rounds down and doesnt wait at all.
     
  2. the_skip

    the_skip Level IV

    Joined:
    Dec 25, 2006
    Messages:
    2,354
    Likes Received:
    1
    Location:
    Indiana
    have a timer called timer with an interval of 1 and a button call button and use this
    Code (Text):
    1.  
    2. Public miliseconds as long
    3. Private sub button_click
    4. timer1.enabled = true
    5. Do until milliseconds = 100
    6. loop
    7. timer1.enabled = false
    8. end sub
    9.  
    10. Private Sub Timer1_Timer()
    11. milliseconds = milliseconds + 1
    12. End Sub[code]This will have the program wait for 100 miliseconds
     
  3. Billy

    Billy Level IV

    Joined:
    Feb 21, 2007
    Messages:
    1,856
    Likes Received:
    39
    all that code does is freeze vb.
     
  4. the_skip

    the_skip Level IV

    Joined:
    Dec 25, 2006
    Messages:
    2,354
    Likes Received:
    1
    Location:
    Indiana
    well put a timer on your form and the interval is the number of miliseconds before the action in the timer takes place. Useually something = something + 1 and put If something >= time you want to wait Then
    do wahtever. Or if it is to record how long something took put Msgbox "it took" + something + " miliseconds to do something
     
  5. ricky92

    ricky92 Administrator
    Staff Member

    Joined:
    Nov 10, 2006
    Messages:
    1,866
    Likes Received:
    67
    Just use this: [edited SecondsToWait]
    Code (Text):
    1. Public Sub MilliSecondsToWait(lNumberOfSeconds As Long)
    2.    Dim ft As FILETIME
    3.    Dim lBusy As Long
    4.    Dim lRet As Long
    5.    Dim dblDelay As Double
    6.    Dim dblDelayLow As Double
    7.    Dim dblUnits As Double
    8.    Dim hTimer As Long
    9.    hTimer = CreateWaitableTimer(0, True, App.EXEName & "Timer")
    10.    If Err.LastDllError = ERROR_ALREADY_EXISTS Then
    11.    Else
    12.        ft.dwLowDateTime = -1
    13.        ft.dwHighDateTime = -1
    14.        lRet = SetWaitableTimer(hTimer, ft, 0, 0, 0, 0)
    15.    End If
    16.    dblUnits = CDbl(&H10000) * CDbl(&H10000)
    17.    If lNumberOfSeconds > 0 Then
    18.        dblDelay = CDbl(lNumberOfSeconds) * 10000
    19.    Else
    20.        dblDelay = 100000
    21.    End If
    22.    ft.dwHighDateTime = -CLng(dblDelay / dblUnits) - 1
    23.    dblDelayLow = -dblUnits * (dblDelay / dblUnits - _
    24.        Fix(dblDelay / dblUnits))
    25.    
    26.    If dblDelayLow < CDbl(&H80000000) Then
    27.        dblDelayLow = dblUnits + dblDelayLow
    28.        ft.dwHighDateTime = ft.dwHighDateTime + 1
    29.    End If
    30.    
    31.    ft.dwLowDateTime = CLng(dblDelayLow)
    32.    lRet = SetWaitableTimer(hTimer, ft, 0, 0, 0, False)
    33.    
    34.    Do
    35.        lBusy = MsgWaitForMultipleObjects(1, hTimer, False, _
    36.            INFINITE, QS_ALLINPUT&)
    37.        DoEvents
    38.    Loop Until lBusy = WAIT_OBJECT_0
    39.    CloseHandle hTimer
    40.  
    41. End Sub
     
  6. Billy

    Billy Level IV

    Joined:
    Feb 21, 2007
    Messages:
    1,856
    Likes Received:
    39
    i did that and now it doesnt freeze, but it doesnt do anything else either. heres my code.
    Code (Text):
    1. Private Sub button_click()
    2. button.Visible = False
    3. Timer1.Enabled = True
    4. Do Until milliseconds = 100
    5. Loop
    6. Timer1.Enabled = False
    7. button.Visible = True
    8. End Sub
    edit:didnt see rickys code... will try now.

    Edit2: ricky, i keep on getting compile errors in your procedure