Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when I interrupt a C# console application with Control-C?

Tags:

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?

like image 641
Colonel Panic Avatar asked Mar 02 '12 14:03

Colonel Panic


People also ask

What happens when there is an interrupt within an interrupt?

If you set the interrupt enable flag within the current interrupt as well, then you can allow further interrupts that are higher priority than the one being executed. This "interrupt of an interrupt" is called a nested interrupt.

Why should the CPU be interrupted?

Why we require Interrupt? External devices are comparatively slower than CPU. So if there is no interrupt CPU would waste a lot of time waiting for external devices to match its speed with that of CPU. This decreases the efficiency of CPU.

What is interrupt in operating system?

What is an interrupt? An interrupt is a signal emitted by a device attached to a computer or from a program within the computer. It requires the operating system (OS) to stop and figure out what to do next. An interrupt temporarily stops or terminates a service or a current process.


1 Answers

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.

like image 60
basti Avatar answered Sep 19 '22 14:09

basti