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

View: 18695|Reply: 1

[Console Program] Console.CancelKeyPress is introduced

[Copy link]
Posted on 1/22/2018 9:25:08 AM | | | |
Use this event in combination with System.ConsoleCancelEventHandler and System.ConsoleCancelEventArgs. The CancelKeyPress event can intercept the Ctrl+C signal so that the event handler can decide whether to continue execution or terminate the console application. For more information about handling events, see Handling and raising events.
When the user presses Ctrl+C or Ctrl+Break, CancelKeyPress triggers the event and the application's ConsoleCancelEventHandler executes the event handler. The event handler passes the ConsoleCancelEventArgs object with two useful properties:
SpecialKey: It determines whether the user calls the handler by pressing Ctrl+C (the property value will be ConsoleSpecialKey.ControlC) or Ctrl+Break (the property value will be ConsoleSpecialKey.ControlBreak).
Cancel, which determines how the application should respond to the user by pressing Ctrl+C or Ctrl+Break. By default, the Cancel property is false for the reason that the program terminates the execution when the handler exits. Changing its property to true specifies that the application should continue to execute.

//
        Summary:
        When System.ConsoleModifiers.Control modify the key (Ctrl) and System.ConsoleKey.C console key
        (C) or Break keys are held down (Ctrl+C or Ctrl+Break) at the same time.
        public static event ConsoleCancelEventHandler CancelKeyPress;

msdn:https://msdn.microsoft.com/zh-cn/library/system.console.cancelkeypress.aspx



In the scenario, we open a thread and write a while(running) deadloop, which can control whether the program runs or not through variables.





Previous:win10 KMS activation is actually a long-term activation
Next:Use python to build "Million Winners" to automatically search for answers
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