locked
using a timer to create a delay RRS feed

  • Question

  • I'm using Visual Basic 2008 Express Edition.

    I'm working on a class.  At various points, I want to insert a delay between executions of the code.   Nothing terribly large.  Around 50-200 miliseconds.  It doesn't seem like much, but it makes the program run better.  I'll probably randomize the length of the delay later between those two marks. 

    So basically the code would go...

    SomeFunction()
    timer delay
    SomeFuntion()


    I have the timer declared as

     

    Private tmrTimer As Timer

    And each place I want to use it, I simply redeclare it.  Or I might add it to the constructor of the class.  Either way.  Just thinking adding it to the class constructor would probably be a better idea.

    tmrTimer =

    New Timer()
    tmrTimer.Interval = 50

    I then use...
    SomeFunction()
    tmrTimer.Enabled = True
    SomeOtherFunction()

    Correct me if I'm wrong, but program flow should go something like this... 

    Somefunction() executes
    timer is enabled
    timer ticks after the specified interval
    timer turns off because of the tick event
    SomeOtherFunction() executes


    For the Tick event created, I use this.  I only want the timer to run once.   If I need it again, I'll reset the interval and re-enable it.   

    Private

     

    Sub tmrTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTimer.Tick

    tmrTimer.Enabled =

    False

     

    End Sub



    Unfortunately, I get the error    "Handles clause requires a WithEvents variable defined in the containing type or one of its base types"


    Now remember, this is all contained in a class, so theoretically speaking I can simply move the class file from project to project and reuse it, without ever having to worry about how the class works.  

    I have a few questions.

    1) Is this the best way to do this?
    2) If it is not the best way, can you point me in a better direction?
    3) Reading through the Timer specifications, I probably need to do some cleanup on the timer after I use it.  Am I correct in this?   

    Thursday, October 15, 2009 5:33 PM

Answers

  • Hi,

    You could use the Sleep method.

    example

    System.Threading.Thread.Sleep(500)


    Here is the MSDN Documentation on the Sleep Method
    http://msdn.microsoft.com/en-us/library/d00bd51t.aspx

    Hope that helps!

    ~Matt

    [edit]

    So in your application it would be something like this.

    DoSomeWork()
    System.Threading.Thread.Sleep(500)
    DoSomeOtherWork()
    • Marked as answer by Jeff Shan Monday, October 19, 2009 6:34 AM
    Thursday, October 15, 2009 5:43 PM
  • If your delays are small, you might consider System.Threading.Thread.Sleep - but, this will block the current thread - so, don't get carried away :)

    You could use a timer as a driver for a state machine - but, you would probably be better off not using the System.Windows.Forms timer.  Maybe System.Timers.Timer or System.Threading.Timer.  These are more appropriate in a non-ui scenario...


    Tom Shelton
    • Marked as answer by Jeff Shan Monday, October 19, 2009 6:34 AM
    Thursday, October 15, 2009 5:49 PM
  • Something like a state macine:

      Dim WithEvents Tmr As New Timer
      Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Tmr.Tag = "Start"
        Tmr.Start()
      End Sub
      Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick
        Tmr.Stop()
        Select Case Tmr.Tag.ToString
          Case "Start"
            SomeSubroutine()
          Case "SomeOtherSubroutine"
            SomeOtherSubroutine()
        End Select
      End Sub
      Sub SomeSubroutine()  'Functions are more difficult.
        Tmr.Tag = "SomeOtherSubroutine"
        Tmr.Interval = 50
        Tmr.Start()
      End Sub
      Sub SomeOtherSubroutine()
    
      End Sub
    
    • Marked as answer by Jeff Shan Monday, October 19, 2009 6:34 AM
    Thursday, October 15, 2009 8:47 PM

All replies

  • Hi,

    You could use the Sleep method.

    example

    System.Threading.Thread.Sleep(500)


    Here is the MSDN Documentation on the Sleep Method
    http://msdn.microsoft.com/en-us/library/d00bd51t.aspx

    Hope that helps!

    ~Matt

    [edit]

    So in your application it would be something like this.

    DoSomeWork()
    System.Threading.Thread.Sleep(500)
    DoSomeOtherWork()
    • Marked as answer by Jeff Shan Monday, October 19, 2009 6:34 AM
    Thursday, October 15, 2009 5:43 PM
  • If your delays are small, you might consider System.Threading.Thread.Sleep - but, this will block the current thread - so, don't get carried away :)

    You could use a timer as a driver for a state machine - but, you would probably be better off not using the System.Windows.Forms timer.  Maybe System.Timers.Timer or System.Threading.Timer.  These are more appropriate in a non-ui scenario...


    Tom Shelton
    • Marked as answer by Jeff Shan Monday, October 19, 2009 6:34 AM
    Thursday, October 15, 2009 5:49 PM
  • the better question would be to ask WHY Does the delay cause the program to run better?


    does it freeze in that method?

    if so you may want to look into using a background worker.
    VB.Net to C# Convert :)
    Thursday, October 15, 2009 8:16 PM
  • Something like a state macine:

      Dim WithEvents Tmr As New Timer
      Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Tmr.Tag = "Start"
        Tmr.Start()
      End Sub
      Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick
        Tmr.Stop()
        Select Case Tmr.Tag.ToString
          Case "Start"
            SomeSubroutine()
          Case "SomeOtherSubroutine"
            SomeOtherSubroutine()
        End Select
      End Sub
      Sub SomeSubroutine()  'Functions are more difficult.
        Tmr.Tag = "SomeOtherSubroutine"
        Tmr.Interval = 50
        Tmr.Start()
      End Sub
      Sub SomeOtherSubroutine()
    
      End Sub
    
    • Marked as answer by Jeff Shan Monday, October 19, 2009 6:34 AM
    Thursday, October 15, 2009 8:47 PM