Link to home
Start Free TrialLog in
Avatar of prakashbe
prakashbe

asked on

Very simple Question...Ans Pls

Hi,
How to insert the time delay between two statement execution in VB

I know in "C" it is "delay(<time in milliseconds>)"
But in VB.....?
Bye
Prakashbe
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Tomlinson
The Sleep() API will delay for the specified time but completely suspends execution of your application making it unresponsive.  

If you need your application to remain responsive during the delay then you can do it this way:

Private Sub delay(seconds As Long)
    Dim endTime As Date
   
    endTime = DateAdd("s", seconds, Now())
    Do While Now() < endTime
        DoEvents
    Loop
End Sub

To demonstrate the difference, create a new project and add two commandButtons.  Hit the first button and then try moving the window around or minimizing and the maximizing it again.  A msgbox will pop up after ten seconds.

Now hit the second commandbutton and try the same thing.  Your app won't respond until the ten seconds are up and the msgbox has been dismissed.

Regards,

Idle_Mind

' Code Follows...
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Command1_Click()
    delay 10
    MsgBox "done"
End Sub

Private Sub delay(seconds As Long)
    Dim endTime As Date
   
    endTime = DateAdd("s", seconds, Now())
    Do While Now() < endTime
        DoEvents
    Loop
End Sub

Private Sub Command2_Click()
    Sleep 10000
    MsgBox "done"
End Sub
The Sleep function freezes your application and won't respond to anything until the time has passed. You can use another method which won't freeze up your application :


Private Sub Form_Load()
Dim T As Double
    T = Timer
    Do Until CLng(Timer - T) = 5
        DoEvents
    Loop
        MsgBox "Finsihed"
End Sub
sleep has same effect as delay in c++

Don't accept this as answer