예제 #1
0
        //fancy select
        public static async Task <long> BigSumIenumerable(IEnumerable <int> delays)
        {
            var tasks = delays.Select(i =>
            {
                Task.Delay(i);
                return(CompletedTask.Get().CalcSomething());
            });

            Task <long>[] calculations = tasks.ToArray();                  //start work
            long[]        ret          = await Task.WhenAll(calculations); //async wait for results

            return(ret.Sum());
        }
예제 #2
0
        public static async Task <long> BigSum()
        {
            List <Task <long> > tasks = new List <Task <long> >();

            foreach (var unused in Enumerable.Range(0, 5))
            {
                tasks.Add(CompletedTask.Get().CalcSomething());
            }

            try
            {
                var ret = await Task.WhenAll(tasks);

                return(ret.Sum());
            }
            catch (Exception e)
            {
                //If any task throws, WhenAll faults with the Exception here
                //There is no need to check exceptions from other tasks; first is enough
                Console.WriteLine(e);
                throw;
            }
        }