We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

Medium Priority
696 Views
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.
Comment
Watch Question

Commented:
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 -

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22041143.html

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
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)
        Do
            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

Example:

    Delay(10) ' hold for ten seconds

Author

Commented:
Microsoft Visual Studio accepted the following code:

System.Threading.Thread.Sleep(5,000)

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

Author

Commented:
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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.