Example #1
0
        public static async Task DoThings()
        {
            // better
            try
            {
                var tasks = new[]
                {
                    DoThing1(),
                    DoThing2(),
                    DoThing3()
                };

                // state.Steps.Completed = 0;
                // state.Steps.Count = tasks.Length;

                Async.WaitAllWithProgress(Console.WriteLine /* steps => state.StepsCompleted = steps */, 0 /* state.StepsCompleted */, steps => steps + 1, tasks);
            }
            catch (AggregateException e)
            {
                Console.WriteLine(string.Join(Environment.NewLine, e.Flatten().InnerExceptions.Select(exception => exception.Message)));
            }

            Console.WriteLine(); // line break

            // better yet
            try
            {
                Async.WaitAllWithProgress(Console.WriteLine /* progress => state.Progress = progress */, new[]
                {
                    DoThing1(),
                    DoThing2(),
                    DoThing3()
                });
            }
            catch (AggregateException e)
            {
                Console.WriteLine(string.Join(Environment.NewLine, e.Flatten().InnerExceptions.Select(exception => exception.Message)));
            }

            Console.WriteLine(); // line break

            // best
            try
            {
                await Async.WhenAllWithProgress(Console.WriteLine /* progress => state.Progress = progress */, new[]
                {
                    DoThing1(),
                    DoThing2(),
                    DoThing3()
                });
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine(string.Join(Environment.NewLine, e.Message));
            }

            Console.WriteLine(); // line break

            // lock
            using (await Stuff.LockAsync())
            {
                // do stuff
            }

            Console.WriteLine(); // line break

            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }