suggestion: support Ctrl+C handling
See original GitHub issueWhen the application is terminated by the user (e.g. presses Ctrl+C
), it terminates immediately.
The application may want to do something, for example: kill some child processes it has started.
It is possible to handle this using Console.CancelKeyPress
.
It would be nicer if this was an API pattern in CommandLineUtils. For example: a CancellationToken
gets passed to OnExecuteAsync
. This token is canceled when the user terminates the application.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:12 (4 by maintainers)
Top Results From Across the Web
Need help handling "CTRL-C" signal in my simple shell ...
From other side, in many cases pressing CTRL+C indicated user wants to terminate program, so quitting the program is expected behavior. Share.
Read more >What to do when Ctrl + C can't kill a process?
If Ctrl + C (SIGINT) doesn't work, try Ctrl + \ (SIGQUIT). Then try Ctrl + Z (SIGTSTP). If that returns you to...
Read more >Ctrl-C
Ctrl -C is just a way to tell your terminal to send a SIGINT signal to the current process. How that process handles...
Read more >Write a C program that does not terminate when Ctrl+C is ...
It prints a message “Cannot be terminated using Ctrl+c” and continues execution. We can use signal handling in C for this.
Read more >Recognizing more `ctrl-` keys in Iex - Elixir Forum
Are “special” characters like tab and ctrl-c handled exclusively by an Erlang server, and somehow Iex's current architecture rules out ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’ve added this to System.CommandLine in https://github.com/dotnet/command-line-api/pull/290. This is the documentation for that: https://github.com/dotnet/command-line-api/wiki/Process-termination-handling.
Interesting idea. I’m thinking through some implementation ideas and it might be quirky when we get into async land. IIRC, callbacks on CancellationTokenSource.Cancel() should run inline, but wouldn’t it still be possible for the CancelKeyPress event handler terminate before some async continuations finish?
The way we solved this in aspnet and dotnet-watch is that the first CTRL+C invocation begins graceful shutdown and sets ConsoleCancelEventArgs.Cancel to true. The second time CTRL+C is raised we force exit the process. I’m wondering if that would be a good pattern to use by default, or if that should be something users configure.