Skip to content

Instantly share code, notes, and snippets.

@mayvazyan
Created May 10, 2013 13:16
Show Gist options
  • Save mayvazyan/5554325 to your computer and use it in GitHub Desktop.
Save mayvazyan/5554325 to your computer and use it in GitHub Desktop.
Stop your console app the nice way
class Program
{
private static readonly AutoResetEvent AutoResetEvent = new AutoResetEvent(false);
static void Main(string[] args)
{
Console.CancelKeyPress += OnCancelKeyPress;
//TODO: do work here
AutoResetEvent.WaitOne();
//TODO: shutdown here
}
private static void OnCancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
e.Cancel = true;
AutoResetEvent.Set();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment