简体   繁体   English

Visual Basic 2012:在循环时立即退出

[英]Visual Basic 2012: Immediately Exiting while Loop

I'm working on a console application for visual basic that will run a piece of code during a while loop. 我正在为Visual Basic开发一个控制台应用程序,它将在while循环中运行一段代码。 If the escape key is pressed I would like the while loop to cease immediately, at any point. 如果按下退出键,我希望while循环在任何时候立即停止。

I'm not sure how I can have the program listen for the escape keypress continuously, without having to put in a million checkpoints. 我不确定如何让程序连续监听逃逸按键,而不必输入一百万个检查点。 I realize that in the below example (just something I wrote in for a sample) that during the "readline" checks I have the opportunity to change the value. 我意识到在下面的示例(只是我为示例编写的内容)中,在“ readline”检查期间,我有机会更改值。 Any help would be appreciated! 任何帮助,将不胜感激!

Dim inp As Char
Dim x, y, z As Integer

While inp <> Chr(27)
    console.writeline ("Press escape at any time to exit")
    console.writeline ("Please enter a numeric value: ")
    x = console.readline()

    If x > 1 Then
        console.writeline ("greater")
    Else
        console.writeline ("lesser")
    End If

    console.writeline ("Do you have other values? Y or N")
    inp = console.readline()

    If inp = "y" Then
        '...other stuff
    Else
        console.writeline ("Press escape to exit or any other key to continue")
        inp = console.readline()
    End If

End While

May be you can also define a public Boolean variable and put it in true when escape key using it key press event. 也许您还可以定义一个公共布尔变量,并在使用键盘按下事件的转义键时将其置于true。 In your loop you could check value of this Boolean in some strategic point and go out case true. 在循环中,您可以在某个战略要点检查此布尔值,并判断为真。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM