Cleanly Handling Ctrl-C In Console Applications
June 4, 2008
I find myself writing Console applications now and again. Command line ninjas know that if you want to stop a running console application you can hit Ctrl-C to exit the application. In our Dovetail Seeker product, which we will be releasing with the next version of Dovetail Agent, when you cancel out of the indexer while it is running it would corrupt the search index. Not a big problem as the search indexes can be easily recreated but that can sometimes take a long time. Besides allowing scenario to exist where the user can corrupt application data is just plain wrong. Luckily it was easy to fill in this hole. My Google-Fu took me to Curt Nichol's post on Ctrl-C and the .NET console application where he introduces the System.Console.CancelKeyPress event and demonstrates using an anonymous method for handling…