We help IT Professionals succeed at work.

VB.NET, Timer creation on the fly with different timer values

I am calling a new class to start a logging session of different devices.

I need to create a timer control along with the sub-code that will be run for EACH timer being created in a loop



    Private Sub create_log_timers()

        Me.SensorMonitorTableAdapter.Fill(Me.Sensormonitor_Dataset.Sensors, True, serial_num)

        Dim x As Integer = 0
        Do Until x = Me.SensorMonitor_DataGridView.RowCount
            int_port = Me.SensorMonitor_DataGridView.Item(1, x).Value                                  ' get port #  - working
            sen_log_time = Me.SensorMonitor_DataGridView.Item(6, x).Value                          ' get port log time ---- working
            Dim y = "textbox" + int_port ' place on value on sensor change event to a text box linked to interface port # - working
            Dim t As New Timer ' Create a timer with port num as name


            ' ----- create timer code to send event to log
'write - private_sub timer(int_port) (by val...) handles timer(int_port).tick
 write  -         senslog.write_s_Log(Date.Now, int_port, sensorInArray(int_port).Text)
write  - end sub
            '-------
            t.Interval = sen_log_time
            t.Enabled = True
            t.Start()

        Loop

    End Sub



Thanks!
Comment
Watch Question

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
You need to associate each Timer with its int_port number?

One way is to simply place the value into the Timers Tag() property.  Use AddHandler() to wire up the Tick() event of all the Timers to the same method.  Cast the "sender" parameter back to the source Timer and then grab the value out of the Tag property.

Something like:
    Private Sub create_log_timers()
        Me.SensorMonitorTableAdapter.Fill(Me.Sensormonitor_Dataset.Sensors, True, serial_num)

        Dim x As Integer = 0
        Do Until x = Me.SensorMonitor_DataGridView.RowCount
            int_port = Me.SensorMonitor_DataGridView.Item(1, x).Value                                  ' get port #  - working
            sen_log_time = Me.SensorMonitor_DataGridView.Item(6, x).Value                          ' get port log time ---- working
            Dim y = "textbox" + int_port ' place on value on sensor change event to a text box linked to interface port # - working

            Dim t As New Timer ' Create a timer with port num as name
            t.Tag = int_port
            AddHandler t.Tick, AddressOf t_Tick
            t.Interval = sen_log_time
            t.Enabled = True
            t.Start()

            ' ...I assume there is more code?...  ("x" isn't getting incremented for example)

        Loop
    End Sub

    Private Sub t_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim t As Timer = CType(sender, Timer)
        Dim int_port As Integer = CType(t.Tag, Integer)
        senslog.write_s_Log(Date.Now, int_port, sensorInArray(int_port).Text)
    End Sub

Open in new window

I am going to try this.

Y,es there is more code to this.  I just need to understand what is going on in the above code and how that is creating separate timers for each port.  let me give it a try and ill get back to you.

Thanks for the help.