コード例 #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");
        }
コード例 #2
0
        private void RunCancellingTasks()
        {
            CancellationTokenSource cts = new CancellationTokenSource();
            CancellationToken       ct  = cts.Token;

            int count = 0;
            int numberOfTasksToRun            = 3;
            List <Task <string> > listOfTasks = new List <Task <string> >();
            SimpleAsyncClass2     SAC2        = new SimpleAsyncClass2();

            for (int index = 0; index < numberOfTasksToRun; index++)
            {
                if (index == 0)
                {
                    listOfTasks.Add(SAC2.LongRunningAsyncMethod1(ct));
                }
                else if (index == 1)
                {
                    listOfTasks.Add(SAC2.LongRunningAsyncMethod2(ct));
                }
                else if (index == 2)
                {
                    listOfTasks.Add(SAC2.ShortRunningAsyncMethod1(ct));
                }
            }

            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);

                count++;

                // cancel after the second task completes
                if (count == 2)
                {
                    // cancel the tasks via the token
                    Console.WriteLine("Cancelling Tasks");
                    cts.Cancel();
                    listOfTasks.Clear();
                }
                else
                {
                    listOfTasks.RemoveAt(completedTaskIndex);
                }
            }

            Console.WriteLine("All tasks complete");
        }
コード例 #3
0
 private void RunTaskExceptionHandling3()
 {
     try
     {
         Console.WriteLine("taskWithException started");
         SimpleAsyncClass2 SAC2 = new SimpleAsyncClass2();
         Task failedTask        = SAC2.TaskMethodThatWillFail();
         failedTask.Wait();
         Console.WriteLine("taskWithException complete");
     }
     catch (AggregateException e)
     {
         Console.WriteLine("AggregateException: " + e.InnerException.Message);
     }
 }
コード例 #4
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);
                }
            }
        }
コード例 #5
0
 public AsyncMenu2()
 {
     _SAC = new SimpleAsyncClass2();
 }
コード例 #6
0
 private void RunPassingParametersToAsyncMethod()
 {
     SimpleAsyncClass2 SAC2 = new SimpleAsyncClass2();
     Task parameterTask     = SAC2.AsyncMethodWithParameter("some string");
 }