tailieunhanh - Apress-Visual CSharp 2010 Recipes A Problem Solution Approach_9

requested became available—this example waits for all of the tasks to complete before obtaining the results. using System; using ; using ; namespace Recipe15_03 { class Recipe15_03 { static void Main(string[] args) { ("Press enter to start"); (); // Create Task Task Task the tasks. task1 = (() = writeDays()); task2 = (() = writeMonths()); task3 = (() = writeCities()); // Wait for all of the tasks to complete. (task1, task2, task3); // Get the results and ("{0} ("{0} ("{0} write them out. days were written", ); months were written", ); cities were written", ); . | CHAPTER 15 PARALLEL PROGRAMMING requested became available this example waits for all of the tasks to complete before obtaining the results. using System using using namespace Recipe15_03 . class Recipe15_03 . static void Main string args Press enter to start Create the tasks. Task int task1 Task int . writeDays Task int task2 Task int . writeMonths Task int task3 Task int . writeCities Wait for all of the tasks to complete. task1 task2 task3 Get the results and write them out. 0 days were written 0 months were written 0 cities were written Wait to continue. nMain method complete. Press Enter static int writeDays . . . . string daysArray Monday Tuesday Wednesday Thursday Friday Saturday Sunday foreach string day in daysArray . . . Day of the Week 0 day 500 . . return 735 CHAPTER 15 PARALLEL PROGRAMMING static int writeMonths string monthsArray Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec foreach string month in monthsArray . . Month 0 month 500 . return static int writeCities . string citiesArray London New York Paris Tokyo Sydney foreach string city in citiesArray . . . City 0 city 500 . . return 15-4. Parallel Process a Collection Problem You need to parallel process each element in a collection. Solution Use the method to create a new task to process each of the elements in a collection. Optionally use to limit the degree of parallelism that will be used. How It Works The static method accepts a collection a function delegate and an optional instance of ParallelOptions as

TỪ KHÓA LIÊN QUAN