我有控制我正在建造的设备的代码。我的主 Sub 向硬件发送几个命令,总是等待必要的时间来执行操作。该算法类似于:
Private Sub StartAuto()
Send command1 to hardware and wait x seconds
Send command2 to hardware and wait y seconds
Send command3 to hardware and wait z seconds
etc
End Sub
问题是,我希望能够使用停止按钮来停止该例程的执行。到目前为止,我想不出或找到比检查每个命令之间的布尔值更好的方法,这将是可怕的:
Private Sub btStopAuto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btStopAuto.Click
keepRunning = false
End Sub
Private Sub StartAuto()
keepRunning = True
Send command1 to hardware and wait x seconds
If (keepRunning = false)
Exit Sub
End If
Send command2 to hardware and wait y seconds
If (keepRunning = false)
Exit Sub
End If
Send command3 to hardware and wait z seconds
If (keepRunning = false)
Exit Sub
End If
etc
End Sub
我考虑过使用While
循环,但它们只有在整个过程完成后才会验证。
本质上我需要的是类似
Private Sub btStopAuto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btStopAuto.Click
StartAuto.Exit
End Sub
显然这不存在,关于如何完成的任何想法?现在,如果我在过程中发现问题,我必须中止调试,但这在开发之外不起作用。
谢谢你。