• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 676
  • Last Modified:

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

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.
0
Baltar
Asked:
Baltar
  • 2
2 Solutions
 
reb73Commented:
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

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
BaltarAuthor 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...
0
 
BaltarAuthor 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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now