Đang chuẩn bị liên kết để tải về tài liệu:
Apress-Visual CSharp 2010 Recipes A Problem Solution Approach_9

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

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

TÀI LIỆU LIÊN QUAN