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

Posted on 2009-02-13
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
    LVL 25

    Accepted Solution

    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 85

    Assisted Solution

    by:Mike Tomlinson
    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

    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

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now