public static async Task WaitFor(int timeoutMs, int taskCount, Action <Action> action, Action onFail = null) { var tsc = new TaskCompletionAwaiter(timeoutMs, taskCount); void Done() { tsc.Tick(); } action(Done); var success = await tsc.Task; if (!success) { var msg = $"Timeout of {timeoutMs}ms exceeded."; if (taskCount > 1) { msg += $" Completed {taskCount - tsc.TaskCount} of {taskCount} tasks."; } onFail?.Invoke(); throw new Exception(msg); } }
/// <summary> /// This method yields the current thread and waits until the whole command queue is processed. /// </summary> public static async Task ProcessCommands(this AblyRealtime client) { var taskAwaiter = new TaskCompletionAwaiter(); #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed _ = Task.Run(async() => { while (true) { await Task.Delay(Timeout); if (client.Workflow.IsProcessingCommands() == false) { taskAwaiter.SetCompleted(); } } }); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed await taskAwaiter.Task; }