Question Continuous timer

RockyBalboa

New member
Joined
Dec 12, 2012
Messages
3
Programming Experience
Beginner
HI,
I am new to VB and i have problem with my timer, I managed to run the timer until enddste 1.
How do I continue beyond that?


this is my code
Public Class Form1

    Dim startdate As DateTime = DateTime.Parse(Date.Now)
    Dim enddate As DateTime = DateTime.Parse("21:53:00")
    Dim enddate1 As DateTime = DateTime.Parse("21:54:00")
    Dim enddate2 As DateTime = DateTime.Parse("21:55:00")
    Dim enddate3 As DateTime = DateTime.Parse("21:56:00")
    Dim enddate4 As DateTime = DateTime.Parse("21:57:00")
    Dim enddate5 As DateTime = DateTime.Parse("21:58:00")
    Dim Flag As Boolean
    Dim ts, ts1, ts2, ts3, ts4, ts5, ts6, ts7, ts8, ts9, ts10, ts11, ts12 As TimeSpan
    Dim ts13, ts14, ts15, ts16, ts17, ts18, ts19, ts20, ts21, ts22, ts23, constant As TimeSpan
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 1000
        Timer1.Enabled = True
        ts = enddate.Subtract(startdate)
        ts1 = enddate1.Subtract(enddate)
        ts2 = enddate2.Subtract(enddate2)
        ts3 = enddate3.Subtract(enddate3)
        ts4 = enddate4.Subtract(enddate4)
        ts5 = enddate5.Subtract(enddate5)
        Flag = True
        Timer1.Start()
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ts = ts.Subtract(New TimeSpan(0, 0, 1)) '~~~ Subtract 1 second
        ts2 = ts2.Subtract(New TimeSpan(0, 0, 1))
        ts4 = ts4.Subtract(New TimeSpan(0, 0, 1))
        '~~~ Display it
        Label1.Text = CStr(ts.Hours) & " Hours: " & CStr(ts.Minutes) & " minuter: " & CStr(ts.Seconds) & " second"
        '~~~ Check if the countdown is finished or not
        If Math.Floor(ts.TotalSeconds) = 0 Then
            Timer1.Stop()
            If Flag = True Then
                ts = ts1
                Flag = False
                Timer1.Start()
            ElseIf Flag = True Then
                ts2 = ts3
                Flag = False
                Timer1.Start()
            ElseIf Flag = True Then
                ts4 = ts5
                Flag = False
                Timer1.Start()
            End If
            MessageBox.Show("Finished !")
        End If
    End Sub

End Class
 
Last edited by a moderator:

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

I think you need to explain in more detail exactly what you are trying to do since the code you have presented is illogical at best since, amongst other things, you are checking a variable called Flag in a conditional statement which will never get executed based on your current coding.

I am guessing that you want to be able to set and then reset a timer to occur at specific times?? If so, then have a go with the code below which will set a timer to occur at the first specified time, based on the current time, and then reset the timer to occur at the next specified time.

On a new form, add a timer, a textbox called txtTimerInterval and a textbox called txtTimerAction with Multiline set to true. Then add the code below to the code behind the form, set the StartTime to the current time on your system and run. Wait a few minutes and see how the project runs.

Code:-

VB.NET:
Public Class Form1
  Dim lstActivationTimes As New List(Of DateTime)
 
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim StartTime As DateTime = DateTime.Parse("09:18:00")
    'set up a list of times to be evaluated based on StartTime
    'Just change this list to the times that need to evaluated.
    'Be sure to put the times in order of occurance since this 
    'routine does NOT accommodate times that are in the wrong order
    With lstActivationTimes
      .Add(StartTime)
      .Add(StartTime.AddMinutes(1))
      .Add(StartTime.AddMinutes(2))
      .Add(StartTime.AddMinutes(3))
      .Add(StartTime.AddMinutes(4))
      .Add(StartTime.AddMinutes(5))
      .Add(StartTime.AddMinutes(6))
      .Add(StartTime.AddMinutes(7))
      .Add(StartTime.AddMinutes(8))
    End With
    SetNextOccuranceOfTimer()
    Timer1.Enabled = True
  End Sub
 
  Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    'run your code here
    txtTimerAction.Text += "Timer Fired @ " & Now.ToLongTimeString & vbCrLf
    SetNextOccuranceOfTimer()
    txtTimerInterval.Text = (Timer1.Interval / 1000).ToString & " seconds"
  End Sub
 
  Private Sub SetNextOccuranceOfTimer()
    Dim ListTimeUsed As Boolean
    'search the list of times to be processed and set the timer interval for the 
    'Timer contriol correctly
    For Each lstTime As DateTime In lstActivationTimes
      If lstTime.TimeOfDay >= Now.TimeOfDay Then
        Timer1.Interval = CInt(lstTime.TimeOfDay.Subtract(Now.TimeOfDay).TotalMilliseconds)
        ListTimeUsed = True
        Exit For
      End If
    Next
    'if the current time is greater than any given time in the list then
    'we calculate the period between now and the first time available toimorrow
    If Not ListTimeUsed Then
      Dim EndOfDay As DateTime = DateTime.Parse("23:59:59 PM")
      Dim StartOfDay As DateTime = DateTime.Parse("00:00:00 AM")
      Timer1.Interval = CInt(EndOfDay.TimeOfDay.Subtract(Now.TimeOfDay).TotalMilliseconds) + CInt(lstActivationTimes.Item(0).TimeOfDay.TotalMilliseconds)
    End If
  End Sub
End Class

Hope that helps.

Cheers,

Ian
 
Top Bottom