Exemple #1
0
        private void RunWaitForTasksToCompleteOneByOne()
        {
            // this is a good pattern to use when:
            // - You are wanting to handle (retry, log error, discard) failed tasks as they happen.
            // - when you want to overlapp computation with result processing


            int numberOfTasksToRun            = 5;
            List <Task <string> > listOfTasks = new List <Task <string> >();
            SimpleAsyncClass2     SAC2        = new SimpleAsyncClass2();

            for (int index = 0; index < numberOfTasksToRun; index++)
            {
                if (index % 2 == 0)
                {
                    listOfTasks.Add(SAC2.AsyncMethod1());
                }
                else
                {
                    listOfTasks.Add(SAC2.AsyncMethod2());
                }
            }

            while (listOfTasks.Count > 0)
            {
                int completedTaskIndex = Task.WaitAny(listOfTasks.ToArray());

                // log errors, retry, skip and move on, etc code here
                Console.WriteLine(listOfTasks[completedTaskIndex].Result);

                listOfTasks.RemoveAt(completedTaskIndex);
            }

            Console.WriteLine("All tasks complete");
        }
Exemple #2
0
        private void RunMultiTaskExceptionHandling()
        {
            try
            {
                Console.WriteLine("Multi Task Exception started");
                SimpleAsyncClass2 SAC2 = new SimpleAsyncClass2();

                Task failedTask     = SAC2.TaskMethodThatWillFail();
                Task failedTask2    = SAC2.TaskMethodThatWillFail2();
                Task successfulTask = SAC2.AsyncMethod1();

                Task[] taskArray = { failedTask, failedTask2, successfulTask };
                Task.WaitAll(taskArray);

                Console.WriteLine("Multi Task Exception complete");
            }
            catch (AggregateException e)
            {
                foreach (Exception ex in e.InnerExceptions)
                {
                    Console.WriteLine("Tasking Error: " + ex.Message);
                }
            }
        }