Timer Question

CJLeit

Freshman
Joined
Feb 1, 2006
Messages
32
I have an application that has to run a test on a piece off eqipment based on 12 time intervals stored in a database. I have the app working but it seems like there must be a better way to do it. Below is a simplified version of what I have now. I would like to have one event handler handle all the timers but the problem is I need to know which timer called the event so I can updated the correct row in the database. Any suggestion would be appreciated.

Code:
Public Class frmMain
    Dim con As New SqlConnection(My.Settings.conString)
    Dim WithEvents timer1 As New System.Timers.Timer
    Dim WithEvents timer2 As New System.Timers.Timer
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim intervalsCommand As New SqlCommand("SELECT * FROM reponse_intervals ORDER BY minutes", con)
        Dim intervals As SqlClient.SqlDataReader
        Dim loopCounter As Int16 = 1

        Using con
            con.Open()
            intervals = intervalsCommand.ExecuteReader
            Do While intervals.Read
                If intervals.Item(0) <> "0" Then
                    timers(loopCounter).Interval = intervals.Item(0)
                End If
            Loop
        End Using
    End Sub

    Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
        timer1.start
	timer2.start
    End Sub

    Private Sub test(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles timer1.Elapsed
	UpdateDBRow(1, ReadSensor)
    End Sub

    Private Sub test(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles timer1.Elapsed
	UpdateDBRow(2, ReadSensor)
    End Sub
End Class
 
One handler, multiple event sources

Use the same event handler for each timer, and use the sender parameter to identify which timer and therefore perform the appropriate action:

Visual Basic:
Public Class frmMain
    Dim WithEvents timer1 As New System.Timers.Timer
    Dim WithEvents timer2 As New System.Timers.Timer
    
    Private Sub test(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles timer1.Elapsed, timer2.Elapsed
        If (sender Is timer1) Then
            UpdateDBRow(1, ReadSensor)
        ElseIf (sender Is timer2) Then
            UpdateDBRow(2, ReadSensor)
        End If
    End Sub
End Class

While this is an improvement, it is still quite crude as it requires you to hard-code the Handles list for all 12 timers, and code 12 conditional tests. An improved approach would be to use an array or collection of timers and programmatically assign the event handler using AddHandler statements. The event handler then need only find the index of sender in the array/collection to update the row.

Off the top of my head:

Visual Basic:
Public Class frmMain
    Dim con As New SqlConnection(My.Settings.conString)
    Dim timerList As New List(Of System.Timers.Timer)()
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '... code ...

        'Create timers
        For i As Integer = 1 To 12
            Dim newTimer As New System.Timers.Timer()

            AddHandler newTimer.Elapsed, AddressOf test
            timerList.Add(newTimer)

            'Perform other timer init here
        Next

        '... code ...
    End Sub

    Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
        For Each t As System.Timers.Timer In timerList
            t.Start()
        Next
    End Sub


    Private Sub test(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
        Dim index As Integer = timerList.IndexOf(sender)
        If (index > -1) Then UpdateDBRow(index + 1, ReadSensor)
    End Sub
End Class

Good luck :cool:
 
Back
Top