简体   繁体   中英

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. If the escape key is pressed I would like the while loop to cease immediately, at any point.

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. 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. In your loop you could check value of this Boolean in some strategic point and go out case true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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