vb.net Timer.Enabled = False 不会停止计时器滴答
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14964697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Timer.Enabled = False does not stop Timer tick
提问by Sang
I have here my code to send SMS:
我在这里有我的代码来发送短信:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
count = DataTable1DataGridView.Rows.Count
For i As Integer = count To 1 Step -1
Try
If SerialPort1.IsOpen Then
'dataGridView1()
' TextBox1.Text = Me.dataGridView1.Rows(0).Cells(2).Value.ToString()
With SerialPort1
.Write("AT" & vbCrLf)
.Write("AT+CMGF=1" & vbCrLf)
.Write("AT+CMGS=" & Chr(34) & DataTable1DataGridView.Rows(i - 1).Cells(2).Value.ToString & Chr(34) & vbCrLf)
.Write(ES_MSG.Rows(0).Cells(0).Value.ToString & Chr(26))
End With
Else
MsgBox("Error on the port selected")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
If (i = count) Then
Exit For
Timer1.Enabled = False
Timer1.Stop()
End If
MsgBox("Message Sent!")
Next
Timer1.Enabled = False
Timer1.Stop()
End Sub
I have enabled timer through a button_click. My problem is timer doesn't seem to stop no matter where I put the Timer.Stop() and Timer.Enabled = False. The worse is when there are errors or when the message is sent, the pop-up seems to appear infinitely even if the count from my datagrid is just small. Can anyone share ideas? I really need it. Thanks.
我通过 button_click 启用了计时器。我的问题是无论我将 Timer.Stop() 和 Timer.Enabled = False 放在哪里,计时器似乎都不会停止。更糟糕的是,当出现错误或发送消息时,即使我的数据网格中的计数很小,弹出窗口似乎也会无限出现。任何人都可以分享想法吗?我真的需要它。谢谢。
回答by Derek Tomes
Firstly, your code after the 'Exit For' will never be called.
首先,永远不会调用“退出”之后的代码。
Secondly, if your timer events are reasonably frequent, you may find that you have queue of the events waiting to be processed and you're mistaking this for the event still firing. I'd suggest that you disable the timer as the first action in your tick event and re-enable it as the last thing on exit if appropriate:
其次,如果您的计时器事件相当频繁,您可能会发现您有等待处理的事件队列,并且您将其误认为是仍在触发的事件。我建议您禁用计时器作为滴答事件中的第一个操作,并在适当的情况下将其重新启用为退出时的最后一件事:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
Dim enableTimer as Boolean = True
count = DataTable1DataGridView.Rows.Count
For i As Integer = count To 1 Step -1
Try
If SerialPort1.IsOpen Then
'dataGridView1()
' TextBox1.Text = Me.dataGridView1.Rows(0).Cells(2).Value.ToString()
With SerialPort1
.Write("AT" & vbCrLf)
.Write("AT+CMGF=1" & vbCrLf)
.Write("AT+CMGS=" & Chr(34) & DataTable1DataGridView.Rows(i - 1).Cells(2).Value.ToString & Chr(34) & vbCrLf)
.Write(ES_MSG.Rows(0).Cells(0).Value.ToString & Chr(26))
End With
Else
MsgBox("Error on the port selected")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
If (i = count) Then
enableTimer = False
Exit For
' This code is never hit
Timer1.Enabled = False
Timer1.Stop()
End If
MsgBox("Message Sent!")
Next
Timer1.Enabled = enableTimer
End Sub
回答by Ruben_PH
I have been using Timer before but I never encountered any problem without using Timer.Stop, also, you may want to change your code to this:
我之前一直在使用 Timer,但在不使用 Timer.Stop 的情况下我从未遇到任何问题,此外,您可能希望将代码更改为:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
count = DataTable1DataGridView.Rows.Count
For i As Integer = count To 1 Step -1
Try
If SerialPort1.IsOpen Then 'I had doubt with this condition, you are testing the same port over and over again
'dataGridView1()
' TextBox1.Text = Me.dataGridView1.Rows(0).Cells(2).Value.ToString()
With SerialPort1
.Write("AT" & vbCrLf)
.Write("AT+CMGF=1" & vbCrLf)
.Write("AT+CMGS=" & Chr(34) & DataTable1DataGridView.Rows(i - 1).Cells(2).Value.ToString & Chr(34) & vbCrLf)
.Write(ES_MSG.Rows(0).Cells(0).Value.ToString & Chr(26))
End With
Else
MsgBox("Error on the port selected")
'Is this an error? You may want to disable your timer here then call Exit Sub
End If
If (i = count) Then Exit For
MsgBox("Message Sent!")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Next
Timer1.Enabled = False
End Sub

