Skip to content

Graceful cancellation

Adam Bajguz edited this page Aug 22, 2020 · 3 revisions

It is possible to gracefully cancel execution of a command and preform any necessary cleanup. By default an app gets forcefully killed when it receives an interrupt signal (Ctrl+C or Ctrl+Break), but you can override this behavior.

In order to make a command cancellation-aware, you need to call console.GetCancellationToken(). This method returns a CancellationToken that will trigger when the user issues an interrupt signal. Note that any code that comes before the first call to GetCancellationToken() will not be cancellation-aware and as such will terminate instantly. Any subsequent calls to this method will return the same token.

[Command("cancel")]
public class CancellableCommand : ICommand
{
    public async ValueTask ExecuteAsync(IConsole console)
    {
        // Make the command cancellation-aware
        var token = console.GetCancellationToken();

        // Execute some long-running operation
        await Task.Delay(Timespan.FromMinutes(10), token);

        console.Output.WriteLine("Done.");
    }
}

Keep in mind that a command may delay cancellation only once. If the user decides to send an interrupt signal (Ctrl+C etc.) again after the first time, the execution will be forcefully terminated regardless of whether the command is cancellation-aware.

Clone this wiki locally