0

基本上,当我继续进入下一个表单时,我的计时器仍在运行,我该如何停止呢?我已经使用了“Timer1.Enabled = False”

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            MsgBox("Sorry, your time has expired. Please try again.", vbOKOnly, "Warning")
            Me.Close()
        End Sub
4

1 回答 1

0

我认为您正在使用 Windows 计时器。

如果这是真的,那么您的第一个消息框会产生不必要的副作用,即在您的程序的消息队列中累积许多 Tick 事件。

您在视频中停留的时间越长,消息框就会显示越多,因为消息在队列中累积,所以会显示更多消息框

您可以尝试使用全局表单变量来解决这种情况

Dim timeElapsed as Boolean 

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
   if timeElapsed = False then
        timeElapsed = true ' It is important that you set immediatly this to true
        MsgBox("Sorry, your time has expired. Please try again.", vbOKOnly, "Warning")
        Me.Close()
        Dim t = CType(sender, Timer)
        t.Elapsed = false
   End If
End Sub

还可以查看 Hans Passant 关于处理计时器的建议

于 2013-05-18T14:42:10.737 回答