How to create multiple timer handler in vb.net

穿精又带淫゛_ 提交于 2019-12-22 01:13:37

问题


  • I have to create multiple timer "n" times with handler.
  • Which will be stored for a row in my DataGrid.
  • For each row there will be a timer that works seperately.

What I thought of looks like:

Private Sub CreateTimer()
    Dim tmr As New Timer
    tmr.Interval = 1000 '1 Second
    tmr.Enabled = True
    AddHandler tmr.Tick, AddressOf GlobalTimerTick
End Sub

'A timer tick handler that would work for each timer I add with the sub above
'All timers I created should work seperately
Private Sub GlobalTimerTick(TheTimer as Timer, ByVal sender As Object, ByVal e As EventArgs)
    mynumber = mynumber + 1
    With DataGridView1
        .Rows(n).Cells(4).Value = mynumber " saniye"
    End With
End Sub

So how can I achieve this?


回答1:


I believe Tag property of Timer would work beautifully in your case. I don't have my IDE currently, but the following snippet should give you the idea.

Private Sub CreateTimer()
    Dim tmr As New Timer
    tmr.Interval = 1000 '1 Second
    tmr.Enabled = True
    tmr.Tag = ROW_INDEX
    AddHandler tmr.Tick, AddressOf GlobalTimerTick
End Sub

'A timer tick handler that would work for each timer I add with the sub above
'All timers I created should work seperately
Private Sub GlobalTimerTick(ByVal sender As Object, ByVal e As EventArgs)
    mynumber = sender.Tag
    With DataGridView1
        .Rows(n).Cells(4).Value = mynumber " saniye"
    End With
End Sub


来源:https://stackoverflow.com/questions/21229684/how-to-create-multiple-timer-handler-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!