I am trying to run my code until Esc was pressed. Therefore I am using ReadKey
in my Console
var input = Console.ReadKey();
do
{
} while (input.Key != ConsoleKey.Escape);
but at "ConsoleKey" it says that, ConsoleKey isn't possible in 'bool'. How can I fix that? Or what shall I use instead?
Try this:
ConsoleKeyInfo input;
do
{
input = Console.ReadKey();
} while (input.Key != ConsoleKey.Escape);
Is there a particular reason you want to use the ESC key instead of the conventional CTRL+C?
You can hook into the Console.CancelKeyPress
event for the latter and it is standard in the command-line interface world.
Console.ReadKey()
is blocking, which can be problematic in some loops. Let's take this example:
using System.Threading;
using System.Threading.Tasks;
CancellationTokenSource cts;
public void Run()
{
cts = new CancellationTokenSource();
var task = new Task(DoSomething, cts.Token);
task.Start();
while (!task.IsCompleted)
{
var keyInput = Console.ReadKey(true);
if (keyInput.Key == ConsoleKey.Escape)
{
Console.WriteLine("Escape was pressed, cancelling...");
cts.Cancel();
}
}
Console.WriteLine("Done.");
}
void DoSomething()
{
var count = 0;
while (!cts.IsCancellationRequested)
{
Thread.Sleep(1000);
count++;
Console.WriteLine("Background task has ticked ({0}).", count.ToString());
}
}
This will do some background work using a Task
, while waiting for ESC to be pressed. Cancellation works just fine, however it will be stuck at the Console.ReadKey()
one more time after completion (cancellation).
You could use Win32 API such as GetKeyboardState
and check the key codes instead, since it is not blocking. However, I recommend using the CancelKeyPress
event instead (CTRL+C):
void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
Console.WriteLine("Cancelling...");
cts.Cancel();
e.Cancel = true; // Do not terminate immediately!
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With