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!
}