Learn how to a build a cloud-first strategyRegister Now


visual basic 2005 timer delay to pause a program for 5 seconds

Posted on 2009-02-13
Medium Priority
Last Modified: 2012-05-06
I want to run a small 5 or 10 second delay in the middle of a sub to keep fat fingers from tripping the event button twice.  I was hoping to do this with just a few lines i can insert.  It's Friday and i can't figure this simple step out.  I'm new to any programming and my background is as a CPA, so be gentle.  Thanks for your help in advance and let me know if there are any specifics i need to provide; my code is useless to provide as i'm just wanting to insert this in a btn activated sub.

I'm using 2005 Visual Basic.
Question by:Baltar
  • 2
LVL 25

Accepted Solution

reb73 earned 750 total points
ID: 23638089
system.thread.sleep(5000)  would cause a 5 second delay, but processing will stop in the current thread.

You might want to look at this solution -


LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 450 total points
ID: 23638423
Here is a more "responsive" delay:

    Private Sub Delay(ByVal DelayInSeconds As Integer)
        Dim ts As TimeSpan
        Dim targetTime As DateTime = DateTime.Now.AddSeconds(DelayInSeconds)
            ts = targetTime.Subtract(DateTime.Now)
            Application.DoEvents() ' keep app responsive
            System.Threading.Thread.Sleep(50) ' reduce CPU usage
        Loop While ts.TotalSeconds > 0
    End Sub


    Delay(10) ' hold for ten seconds

Author Comment

ID: 23652208
Microsoft Visual Studio accepted the following code:


I wanted the sub to completely stop during the 5 second interval and this worked...

Author Closing Comment

ID: 31546805
I used the reb73 approach but the exact script of Idle Mind regarding the extra namespace location of "Threading".  I follow the Idle Mind approach but to be honest don't understand it's added benefit so I used the simplier method.  I increased the overall value of the points so dividing them wouldn't make them insignificant.  Thanks though, i'd rather increase my knowledge then just copy/paste and you guys helped with that.

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

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 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