如何在没有延迟的情况下暂停循环内的动画

时间:2014-12-02 15:01:16

标签: vb.net

我遇到了这段代码的问题:


    Do While StopProgram = False
        Do Until Count = v
            Application.DoEvents()
            Do While StopProgram = False
                If DirectionNegative = False Then
                    Me.Refresh()
                    Count += 1
                    Angle += 1
                    RadianAngle = Angle * PlaceHolder
                    If Angle >= 51 Then
                        Angle = 49
                        DirectionNegative = True
                    End If
                ElseIf DirectionNegative = True Then
                    Me.Refresh()
                    Count += 1
                    Angle -= 1
                    RadianAngle = Angle * PlaceHolder
                    If Angle <= -51 Then
                        Angle = -49
                        DirectionNegative = False
                    End If
                End If
            Loop
        Loop
            Count = 0
        Loop

问题在于,当我按下表单上的按钮,停止代码(使用StopProgram = False)时,它将一直持续到Count = v我希望能够让我停下来它在循环中,我试图在这里做到这一点,但它创建了一个不可用的循环,它破坏了程序。

4 个答案:

答案 0 :(得分:0)

更好的是,使用Timer定期打勾。

http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

Dim WithEvents t为New System.Timers.Timer

sub t_Elapsed Handles ....

&#39;您的代码在这里。递增/递减状态变量 结束子

当您想要停止动画时,只需禁用计时器。

答案 1 :(得分:0)

这是解决问题的错误方法,但更改:

Do While StopProgram = False
    Do Until Count = v
        Application.DoEvents()
        Do While StopProgram = False

要:

Do While StopProgram = False
    Do Until Count = v
        Do While StopProgram = False
            Application.DoEvents()

答案 2 :(得分:0)

你可以删除2个外圈 除非Do Until Count = v为真,否则您永远不会到达StopProgram部分。 您将保留在内部Do While StopProgram = False循环内。你可以删除 内在Do While StopProgram = False loop并改变 Do Until Count = vDo Until Count = vStopProgram = true

当你退出中间循环时你正在设置Count = 0所以如果你需要在StopProgram设置为true时保留count值,你应该删除外部 Do While StopProgram = False loopCount = 0

Do Until Count = v Or StopProgram = True
    Application.DoEvents()
    If DirectionNegative = False Then
        Me.Refresh()
        Count += 1
        Angle += 1
        RadianAngle = Angle * PlaceHolder
        If Angle >= 51 Then
            Angle = 49
            DirectionNegative = True
        End If
    ElseIf DirectionNegative = True Then
        Me.Refresh()
        Count += 1
        Angle -= 1
        RadianAngle = Angle * PlaceHolder
        If Angle <= -51 Then
            Angle = -49
            DirectionNegative = False
        End If
    End If
Loop

答案 3 :(得分:0)

这应该可以正常工作。不需要所有这些循环。

Public Sub Refresh()
  ' ...
End Sub

Public Property Get StopProgram() As Boolean
    StopProgram = ' ...
End Property

Public Property Get Angle as Single
'...
Public Property Let Angle
'...

Public Property Get RadianAngle as Single
    RadianAngle = Angle * PlaceHolder
End Property

Public Sub T()

    Dim Step As Single        
    Step = 1!
    Do
        DoEvents
        Me.Refresh
        If StopProgram Then Exit Do
        If Angle <= 0 Or Angle >= 50 Then
            Step = -Step
        End If
        Count = Count + 1
        Angle = Angle + Step
    Loop

End Sub