This article is a mirror article of machine translation, please click here to jump to the original article.

View: 20154|Reply: 0

[Tips] C# handles console shutdown events

[Copy link]
Posted on 7/8/2020 3:53:04 PM | | |
Application scenarios

The console application we developed is likely to be terminated by the user Ctrl+C or closed by the user during the running phase. If we do not want the user to terminate our program with Ctrl+C, we need to deal with the Ctrl+C or close event.
Treatment method

On the .net platform, there is a CancelKeyPress event in the Console class that can handle Ctrl+C, but this processing is not effective for closing the console application directly.

However, there is a SetConsoleCtrlHandler function in the Windows API that handles both closure events.

The C# processing code is as follows:






Previous:【Primeng】Solution to the problem that the p-calendar date picker is obscured by modal boxes
Next:Cannot load Counter Name data because an invalid index ...
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com