Question change timer interval without restarting

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
If you change the timer's interval while its running, it also restart the timer, right? Can you change the interval without restart the timer?
 
what do you mean? can you give an example?
If i have a timer with interval of 1hr. After its running for 30min. I change the interval to 2hr with a preprogrammed numericupdown box. I want the timer to tick 1h30min from now and every 2hr onward. instead of 2hr from now. Do i need to set the interval to 1h30min and when it tick, set it to 2hr again?
 
Ah right my apologies I get you now.. Yes, if you know how long it has been since the last tic, you need to first set interval to the difference, which will restart the timer, then trigger the new interval when the timer executes the code..
 
Hi,

There is no value that can be evaluated in a timer to see how far along a timer has gone between it's tick events. I have therefore had a play this morning and created a new timer class called MyTimer which uses an additional embedded timer to basically create a tick count. In fact it counts in seconds but should be fine for what you need.

It can effectively handle an increase of the Interval property of the timer by setting the new property myNewTimer.IntervalOverrider = 5000. When set, this property checks whether the current Interval needs to extended to accommodate the new interval. Once the extension has been reached then the timer Interval property is then updated with IntervalOverrider value.

There may still be a few bugs to work out and I am not sure how efficient a timer within a timer really is but to give it a try add two buttons to a new form, create a textbox with multiline set to true and copy the code below to the code behind the form.

VB.NET:
Public Class Form1
  Dim myNewTimer As New MyTimer
  Dim CheckTime As DateTime
 
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    AddHandler myNewTimer.Tick, AddressOf myTickHandler
 
    With myNewTimer
      .Interval = 10000
      .Enabled = True
    End With
    CheckTime = Now
  End Sub
 
  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    CheckTime = Now
    myNewTimer.IntervalOverrider = 20000
  End Sub
 
  Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    CheckTime = Now
    myNewTimer.IntervalOverrider = 5000
  End Sub
 
  Private Sub myTickHandler(sender As System.Object, e As System.EventArgs)
    Dim TS As New TimeSpan
    TS = Now.Subtract(CheckTime)
    TextBox1.Text += TS.Seconds.ToString & vbCrLf
    CheckTime = Now
  End Sub
End Class
 
Public Class MyTimer
  Inherits Timer
  Private Const OneSec As Integer = 1000
  Private WithEvents TickTimer As New Timer
  Private Property ElapsedTicks As Integer
  Private _IntervalOverrider As Integer
 
  Public Sub New()
    With TickTimer
      .Interval = OneSec
    End With
  End Sub
 
  Public Property IntervalOverrider As Integer
    Get
      Return _IntervalOverrider
    End Get
    Set(value As Integer)
      _IntervalOverrider = value
      If Not _IntervalOverrider = MyBase.Interval Then
        If _IntervalOverrider > MyBase.Interval Then
          MyBase.Interval = _IntervalOverrider - ElapsedTicks
        Else
          MyBase.OnTick(New System.EventArgs)
          MyBase.Interval = _IntervalOverrider
        End If
      End If
    End Set
  End Property
 
  Public Overrides Property Enabled As Boolean
    Get
      Return MyBase.Enabled
    End Get
    Set(value As Boolean)
      MyBase.Enabled = value
      TickTimer.Enabled = value
    End Set
  End Property
 
  Private Sub CheckForChangedElapsedTime() Handles MyBase.Tick
    ElapsedTicks = 0
    If IntervalOverrider > 0 AndAlso Not IntervalOverrider = MyBase.Interval Then
      MyBase.Interval = IntervalOverrider
    End If
  End Sub
 
  Private Sub TickTimer_Tick(sender As System.Object, e As System.EventArgs) Handles TickTimer.Tick
    ElapsedTicks += OneSec
  End Sub
End Class
Hope that helps.

Cheers,

Ian
 
The way I did my timer was saving Now in the timer.tag each time it ticks. So I can check the elapse time.
 
Back
Top