Timer.Enabled = False不会停止计时器滴答

时间:2013-02-19 18:24:44

标签: vb.net timer

我这里有我的代码发送短信:

 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

我通过button_click启用了计时器。我的问题是计时器似乎没有停止,无论我把Timer.Stop()和Timer.Enabled = False放在哪里。更糟糕的是当出现错误或发送消息时,即使来自我的数据网格的计数很小,弹出窗口也似乎无限出现。谁能分享想法?我真的需要它。感谢。

2 个答案:

答案 0 :(得分:2)

首先,永远不会调用'Exit For'之后的代码。

其次,如果您的计时器事件相当频繁,您可能会发现您有等待处理事件的队列,并且您错误地将此事件用于仍然触发的事件。我建议您将计时器作为tick事件中的第一个操作禁用,并在适当时重新启用它作为退出时的最后一个操作:

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

答案 1 :(得分:0)

之前我一直在使用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