What happens when I interrupt my C# console application with Control-C?
Is the process killed? Is memory freed? Are finally
blocks executed? What happens to database connections?
Does any of this differ if the application is built for debug or release, or run inside/outside Visual Studio?
Short Answer: It's doing nothing after CTRL-C
Long Answer:
There is good article about it on the MSDN which clearly states, that it sends a signal (interrupt) instead of keypress-events.
There is also a cancelKeyPress-Event triggered which you can subscribe to and do whatever you want!
Unluckily there is no more info about what's actually done by default. Maybe in the worst case you can check it out by yourself. But imo there should be some documentation about it...
UPDATE: Alois Kraus wrote a codeproject-Article about gracefully shuting down a console-application after receiving CTRL-C.
To quote Alois Kraus:
The default behavior of the CLR is to do nothing. This does mean that the CLR is notified very late by a DLL_PROCESS_DETACH notification in which context no managed code can run anymore because the OS loader lock is already taken. We are left in the unfortunate situation that we get no notification events nor are any finalizers run. All threads are silently killed without a chance to execute their catch/finally blocks to do an orderly shutdown. I said in the first sentence default, because there is a way to handle this situation gracefully. The Console class has got a new event member with .NET 2.0: Console.CancelKeyPress. It allows you to get notified of Ctrl-C and Ctrl-Break keys where you can stop the shutdown (only for Ctrl-C, but not Ctrl-Break). The main problem here is if you catch the Ctrl-C/Break event and exit the handler there are no finalizers called. This is not what I would call a cooperative shutdown. The first thing that comes to my mind is to call Environment.Exit but it does not trigger any finalizers. All is not lost. I did come up with a dirty trick to run all finalizers: we spin up a little helper thread inside the event handler which will then call Environment.Exit. Voila, our finalizers are called.