Using async/await is so fun but in the same time there are plenty of situations which the awaiting part can stuck forever, specially when there is any involvement of programming over the network. Other than that there are situations which the app must not wait more than a timeout for a response.
Therefore, the ability to set a timeout for a asynchronous operation is a must, by using CancellationToken one can achieve this and this post will demonstrate an easier method of using these CancellationTokens by applying an extension method for the Task object type.
Let's say there is a Task which it can take unknown amount of time to finish
private Task Task_NetworkBound()
{
return new HttpClient().GetStringAsync("https://dotnetfoundation.org");
}
private Task<string> Task_NetworkBound_T()
{
return new HttpClient().GetStringAsync("https://dotnetfoundation.org");
}
In order for the task to get canceled after 1 seconds
Install it from Nuget
dotnet add package easyAsyncCancel
var cts = new CancellationTokenSource();
cts.CancelAfter(1000);
await Task_NetworkBound().CancelWith(cts.Token);
await Task_NetworkBound_T().CancelWith(cts.Token,"Custom Message");
await Task_NetworkBound_T().CancelAfter(1000);
await Task_NetworkBound().CancelAfter(1000,"Custom Message");
Happy coding :)