Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

 

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

Posted

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:

 

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:

 

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:

Never trouble another for what you can do for yourself.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...