C# exit if key is pressed

Viewed 472

I have an issue with the following code:

using System;
class Yellow
{
    static void Main()
    {
        Console.WriteLine("Check_1");
        Console.ReadKey();
        Console.WriteLine("Check_2");
        char beep = '\a';
        int i = 3;
        while (!Console.KeyAvailable)
        {
            Console.WriteLine(beep);
            System.Threading.Thread.Sleep(200);
            Console.WriteLine("Check_" + i);
            i++;
        }
    }
}

After waiting for an input character to start, this program outputs a beep and a message of Check_<i> with a wait of 200 milliseconds between each loop. If a key is pressed during that loop, the program will terminate.

However, when I wait for the loop to have run many times (around 36 times), the loop doesn't break when I press a key. When I try to manually close the console with the X button, the program is non-responsive.

Why would it matter if the loop had run a few dozen times?

0 Answers
Related