created: 2022-08-15 updated: 2022-08-18

.NET

Decompile

CancellationToken

Use token to cancel long-running tasks.

CancellationTokenSource tokenSource = new();

try
{
    await httpClient.GetAsync("http://someApi/endpoint", tokenSource.Token)
}
catch (TokenCancellationException ex)
{
    logger.LogInformation(ex, "token cancelled");
}
catch (Exception ex)
{
    throw;
}

Tags