What is the best way to keep a console application open as long as the CancelKeyPress event has not been fired?
I would prefer to not use Console.Read or Console.ReadLine as I do not want to accept input. I just want to enable the underlying application to print to the console event details as they are fired. Then once the CancelKeyPress event is fired I want to gracefully shut down the application.
I’m assuming that ‘gracefully shut down the application’ is the part you are struggling with here. Otherwise your application will automatically exit on ctrl-c. You should change the title.
Here’s a quick demo of what I think you need. It could be refined a bit more with use of locking and Monitors for notification. I’m not sure exactly what you need though, so I’ll just pose this…
The _s_stop boolean should be declared volatile or an overly-ambitious optimizer might cause the program to loop infinitely.