0

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

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 放在哪里,计时器似乎都不会停止。更糟糕的是,当出现错误或发送消息时,即使我的数据网格中的计数很小,弹出窗口似乎也会无限出现。任何人都可以分享想法吗?我真的需要它。谢谢。

4

2 回答 2

2

首先,永远不会调用“退出”之后的代码。

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

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
于 2013-02-19T20:38:26.243 回答
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
于 2013-02-19T20:12:43.253 回答