Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
TaskExtensions.cs
2{
6 public static class TaskExtensions
7 {
15 public static async Task<TResult> TimeoutAfter<TResult>(this Task<TResult> task, TimeSpan timeout)
16 {
17 using CancellationTokenSource tcs = new();
18 Task completedTask = await Task.WhenAny(task, Task.Delay(timeout, tcs.Token));
19 if (completedTask == task)
20 {
21 tcs.Cancel();
22 return await task; // Very important in order to propagate exceptions
23 }
24 throw new TimeoutException();
25 }
26 }
27}
An extensions class for the Task class.
static async Task< TResult > TimeoutAfter< TResult >(this Task< TResult > task, TimeSpan timeout)
Helper method to wait for a task to complete, but with a given time limit.