/// <summary> /// Repeats given action until the given task/awaitable completes. /// The optional final action gets called with the task's result. /// </summary> public static async Task RepeatUntilCompleted <T>(this IAwaitable <T> self, Func <Task> action, Func <T, Task> finallyAction = null) { while (!self.IsCompleted) { await Await.WhenAny(action().AsAwaitable(), self); } if (finallyAction != null) { await finallyAction(self.Result); } }
/// <summary> /// Repeats given action until the given task/awaitable completes. /// </summary> public static async Task RepeatUntilCompleted(this IAwaitable self, Func <Task> action, Action finallyAction) { while (!self.IsCompleted) { await Await.WhenAny(action().AsAwaitable(), self); } if (finallyAction != null) { finallyAction(); } }
static async void Run(Awaitable <int> a, Awaitable <int> b, CancellationToken ct) { try { var i = await Await.WhenAny(a, b).WithCancellation(ct); Console.WriteLine("got: {0}", i); } catch (OperationCanceledException) { Report.Line("cancelled"); } }
/// <summary> /// Repeats given action until the next value of this event source arrives. /// The optional final action gets called with this newly arrived value. /// </summary> public static async Task RepeatUntilNext <T>(this IEvent <T> self, Func <Task> action, Action <T> finallyAction) { var next = self.Next; while (!next.IsCompleted) { await Await.WhenAny(action().AsAwaitable(), next); } if (finallyAction != null) { finallyAction(next.Result); } }