void ExecuteBuildTasks(SingleThreadedSynchronizationContext context, List <Exception> exceptions) { Task [] tasks = new Task [Count]; for (int i = 0; i < Count; i++) { tasks [i] = this [i].Execute(this); } Task.Factory.StartNew(async() => { try { await Task.WhenAll(tasks); } catch (Exception e) { exceptions.Add(e); } finally { context.SetCompleted(); } }, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext()); }
public void Execute() { if (Count == 0) { return; } var savedContext = SynchronizationContext.Current; var exceptions = new List <Exception> (); try { var context = new SingleThreadedSynchronizationContext(); SynchronizationContext.SetSynchronizationContext(context); ExecuteBuildTasks(context, exceptions); context.Run(); } finally { SynchronizationContext.SetSynchronizationContext(savedContext); } if (exceptions.Count > 0) { throw new AggregateException(exceptions); } }