How can I continue to run my console application until a key press (like Esc is pressed?)
I\'m assuming its wrapped around a while loop. I don\'t like
The shortest way:
Console.WriteLine("Press ESC to stop");
while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape))
{
// do something
}
Console.ReadKey()
is a blocking function, it stops the execution of the program and waits for a key press, but thanks to checking Console.KeyAvailable
first, the while
loop is not blocked, but running until the Esc is pressed.