Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Very simple Question...Ans Pls

Posted on 2003-12-01
4
355 Views
Last Modified: 2010-05-01
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
0
Comment
Question by:prakashbe
4 Comments
 
LVL 39

Accepted Solution

by:
appari earned 50 total points
ID: 9855929
use sleep API

add the API declaration
Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)

call it like
sleep 10000 '**** Sleeps for 10 Sec.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9856091
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
0
 
LVL 14

Expert Comment

by:aelatik
ID: 9856127
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
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 9857749
sleep has same effect as delay in c++

Don't accept this as answer
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question