Ejemplo n.º 1
0
        private static void HandleAggregateTaskException(Task whenAllTask, Exception exception)
        {
            if (whenAllTask?.Exception != null)
            {
                PlaygroundUtils.PrintExceptionInfo(whenAllTask.Exception);

                Console.WriteLine($"Collected Exceptions ({whenAllTask.Exception?.InnerExceptions.Count}):");

                foreach (var inner in whenAllTask.Exception !.InnerExceptions)
                {
                    PlaygroundUtils.PrintExceptionInfo(inner);
                }
            }

            if (exception != null)
            {
                Console.WriteLine("Thrown directly:");
                PlaygroundUtils.PrintExceptionInfo(exception);
            }

            if (whenAllTask?.Exception == null && exception == null)
            {
                Console.WriteLine("No exception caught.......");
            }
        }
Ejemplo n.º 2
0
        public static async Task RunSingleTask()
        {
            PlaygroundUtils.Start("Single");

            try
            {
                // throws
                await RunAsync(0);
            }
            catch (Exception e)
            {
                PlaygroundUtils.PrintExceptionInfo(e);
            }
        }
Ejemplo n.º 3
0
        public static Task RunSingleVoidTask()
        {
            PlaygroundUtils.Start("Single-Void");

            try
            {
                // throws
                FailingService.ThrowVoid();
            }
            catch (Exception e)
            {
                PlaygroundUtils.PrintExceptionInfo(e);
            }

            return(Task.CompletedTask);
        }
Ejemplo n.º 4
0
        public static async Task RunMultipleTasks()
        {
            PlaygroundUtils.Start("Multiple");

            try
            {
                // throws exception with ms=50
                var task1 = FailingService.ThrowAfter(10);
                var task2 = FailingService.ThrowAfter(50);

                await task2;
                await task1;
            }
            catch (Exception e)
            {
                PlaygroundUtils.PrintExceptionInfo(e);
            }
        }
Ejemplo n.º 5
0
        public static async Task RunWhenAll()
        {
            PlaygroundUtils.Start("When-All");

            try
            {
                var taskList = new[]
                {
                    Task.Run(() => RunAsync(1)),
                    Task.Run(() => RunAsync(2)),
                    Task.Run(() => RunAsync(3)),
                };

                // throws exception with i=1
                await Task.WhenAll(taskList);
            }
            catch (Exception e)
            {
                PlaygroundUtils.PrintExceptionInfo(e);
            }
        }
Ejemplo n.º 6
0
        public static async Task RunAggregateNonAwaited()
        {
            PlaygroundUtils.Start("Aggregate Non-Awaited");

            Task whenAll = null;

            try
            {
                var tasks = new[]
                {
                    FailingService.ThrowSomething("instant awaited"),
                    FailingService.ThrowAfter(120)
                };

                whenAll = Task.WhenAll(tasks);
                await whenAll;
            }
            catch (Exception e)
            {
                HandleAggregateTaskException(whenAll, e);
            }
        }