How to handle “End Task” from Task Manager in .NET 5 console app?
问题 My .NET 5 console application registers a HandlerRoutine by using SetConsoleCtrlHandler, so it can do some cleanup before exiting. This allows me to react to CTRL+C / CTRL+BREAK , ALT+F4 and the console being closed using the X button. Sadly, the HandlerRoutine doesn't get called when Task Manager tries to terminate the application after clicking End Task in the Process tab, even though the documentation for HandlerRoutine states the following regarding CTRL_CLOSE_EVENT : A signal that the