17 using CancellationTokenSource tcs =
new();
18 Task completedTask = await Task.WhenAny(task, Task.Delay(timeout, tcs.Token));
19 if (completedTask == task)
24 throw new TimeoutException();
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.