0
            For i = 1 To 10
                Ascii()
                ForegroundColor = random.Next(1, 16)
                DrawSlot("Unknown")
                WriteLine()
                ForegroundColor = random.Next(1, 16)
                DrawSlot("Unknown")
                WriteLine()
                ForegroundColor = random.Next(1, 16)
                DrawSlot("Unknown")
                Thread.Sleep(100)
                Clear()
            Next

I want this loop to continue until the user presses a button on their keyboard, causing the loop to end. Using a console.readline causes the program to pause and wait for an input, so I was wondering if there was another way of doing it?

Adam
  • 13
  • 2
  • 1
    How are you displaying images with a console app? Show us some code please... – Idle_Mind Dec 09 '20 at 18:49
  • @Idle_Mind added code. Sorry – Adam Dec 09 '20 at 19:02
  • You want [Console.KeyAvailable](https://learn.microsoft.com/en-us/dotnet/api/system.console.keyavailable?view=net-5.0) and [Console.ReadKey()](https://learn.microsoft.com/en-us/dotnet/api/system.console.readkey?view=net-5.0). See [my snake game](https://stackoverflow.com/a/16907575/2330053) as an example. – Idle_Mind Dec 09 '20 at 19:15
  • Also, instead of clearing the entire screen from within the loop, I'd clear it before the loop and use [Console.SetCursorPosition()](https://learn.microsoft.com/en-us/dotnet/api/system.console.setcursorposition?view=net-5.0) to move the cursor where you want to draw. This should be faster overall. You can hide the cursor moving around with [Console.CursorVisible](https://learn.microsoft.com/en-us/dotnet/api/system.console.cursorvisible?view=net-5.0). Instead of a `for` loop, use a `while` loop as well. – Idle_Mind Dec 09 '20 at 19:20
  • @Idle_Mind thanks for the help :D – Adam Dec 09 '20 at 19:29

1 Answers1

0

Here's a simple example using the functions I linked to in the comments:

Sub Main(args As String())

    ' ... possibly other code ...

    Console.Clear()
    Console.CursorVisible = False

    Console.WriteLine("Press any key to cancel processing.")
    Console.Write("Processing...")
    Dim top As Integer = Console.CursorTop
    Dim left As Integer = Console.CursorLeft

    Dim progress As String = "/-\|"
    Dim sequence As IEnumerator(Of Char) = progress.GetEnumerator

    ' clear the buffer out
    While Console.KeyAvailable
        Console.ReadKey(True)
    End While

    ' keep "processing" until a key is pressed
    While Not Console.KeyAvailable
        If Not sequence.MoveNext Then
            sequence = progress.GetEnumerator
            sequence.MoveNext()
        End If

        Console.SetCursorPosition(left, top)
        Console.Write(sequence.Current)

        System.Threading.Thread.Sleep(100)
    End While
    ' consume the keypress that cancelled the loop above
    ' without displaying it
    Dim cki As ConsoleKeyInfo = Console.ReadKey(True)

    Console.WriteLine()
    Console.WriteLine("Processing cancelled!")
    Console.WriteLine("Cancelled with: " & cki.KeyChar)

    Console.CursorVisible = True
    Console.WriteLine()
    Console.Write("Press Enter to Quit...")
    Console.ReadKey()
End Sub

Output:

enter image description here

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40