public static void PerformAsyncProgress <T1, T2>(this T1 cmdlet, IProgressKeeper keeper, IList <Task <T2> > taskList, Func <T2, ErrorRecord> actionOnError, int threadSleep = 100) where T1 : Cmdlet { ProgressRecord pr = keeper.GetRecord(0); cmdlet.WriteProgress(pr); while (taskList.Count > 0) { for (int i = taskList.Count - 1; i >= 0; i--) { Task <T2> task = taskList[i]; if (task.IsCompleted) { if ((task.IsFaulted || task.IsCanceled) && task.Result != null) { cmdlet.WriteError(actionOnError(task.Result)); } else { WriteExtensions.WriteError(cmdlet, "No task result was received.", typeof(InvalidOperationException), ErrorCategory.InvalidOperation, task); } } } Thread.Sleep(threadSleep); keeper.Continue(keeper.TotalCount - taskList.Count, ref pr); cmdlet.WriteProgress(pr); } }
public static List <T2> PerformAsyncProgressAndReturn <T1, T2>(this T1 cmdlet, IProgressKeeper keeper, IList <Task <T2> > taskList, Func <Task <T2>, bool> successCondition, Func <Task <T2>, ErrorRecord> actionOnError, int threadSleep = 100) where T1 : Cmdlet { var list = new List <T2>(taskList.Count); ProgressRecord pr = keeper.GetRecord(0); cmdlet.WriteProgress(pr); while (taskList.Count > 0) { for (int i = taskList.Count - 1; i >= 0; i--) { Task <T2> task = taskList[i]; if (task.IsCompleted) { if (successCondition(task)) { list.Add(task.Result); } else { cmdlet.WriteError(actionOnError(task)); } taskList.Remove(task); } } Thread.Sleep(threadSleep); keeper.ContinueReverse(taskList.Count, ref pr); cmdlet.WriteProgress(pr); } keeper.Finish(ref pr); cmdlet.WriteProgress(pr); return(list); }