I want to wait for a Task
A generic version of @Kevan's answer above, using Reactive Extensions.
public static Task TimeoutAfter(this Task task, TimeSpan timeout, IScheduler scheduler)
{
return task.ToObservable().Timeout(timeout, scheduler).ToTask();
}
With optional Scheduler:
public static Task TimeoutAfter(this Task task, TimeSpan timeout, Scheduler scheduler = null)
{
return scheduler is null
? task.ToObservable().Timeout(timeout).ToTask()
: task.ToObservable().Timeout(timeout, scheduler).ToTask();
}
BTW: When a Timeout happens, a timeout exception will be thrown