Danh mục

Addison essential C# 4.0 Visual Studio_9

Số trang: 98      Loại file: pdf      Dung lượng: 1.75 MB      Lượt xem: 10      Lượt tải: 0    
Hoai.2512

Phí tải xuống: 26,000 VND Tải xuống file đầy đủ (98 trang) 0
Xem trước 10 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu addison essential c# 4.0 visual studio_9, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Addison essential C# 4.0 Visual Studio_9 737 R unning LINQ Queries in ParallelListing 18.17: Canceling a Parallel Loop using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; public class Program { public static List ParallelEncrypt( List data, CancellationToken cancellationToken) { return data.AsParallel(). WithCancellation( cancellationToken) .Select( (item) => Encrypt(item)).ToList(); } public static void Main() { List data = Utility.GetData(1000000).ToList(); CancellationTokenSource cts = new CancellationTokenSource(); Console.WriteLine(Push ENTER to exit.); Task task = Task.Factory.StartNew(() => { data = ParallelEncrypt(data, cts.Token); } , cts.Token); / / Wait for the users input Console.Read(); cts.Cancel(); Console.Write(stars); try{task.Wait();} catch (AggregateException){} } / / ... }OUTPUT 18.8: ERROR: The operation was canceled. From the Library of Wow! eBook738 C hapter 18: Multithreading As with a parallel loop, canceling a PLINQ query requires a Cancella- tionToken, which is available on a CancellationTokenSource.Token prop- erty. However, rather than overloading every PLINQ query to support the cancellation token, the ParallelQuery object returned by IEnumera- ble’s AsParallel() method includes a WithCancellation() extension method that simply takes a CancellationToken. As a result, calling Can- cel() on the CancellationTokenSource object will request the parallel query to cancel—because it checks the IsCancellationRequested property on the CancellationToken. As mentioned, canceling a PLINQ query will throw an exception in place of returning the complete result. Therefore, all canceled PLINQ que- ries will need to be wrapped by try{…}/catch(OperationCanceledExcep- tion){…} blocks to avoid an unhandled exception. Alternatively, as shown in Listing 18.17, pass the CancellationToken to both ParallelEncrypt() and as a second parameter on StartNew(). This will cause task.Wait() to throw an AggregateException whose InnerException property will be set to a TaskCanceledException. Multithreading before .NET Framework 4 TPL is a fantastic library covering a multitude of multithreading patterns with extensibility points to handle even more. However, there is one sig- nificant drawback to TPL: It is available only for the .NET Framework 4 or for use with the Rx library in .NET 3.5. In this section, we cover multi- threading technology before TPL. Asynchronous Operations with System.Threading.Thread Listing 18.18 (with Output 18.9) provides an example. Like TPL, there is a fundamental type, System.Threading.Thread, which is used to control an asynchronous operation. Like System.Threading.Tasks.Task in TPL, Thread includes a Start method and a wait equivalent, Join(). Listing 18.18: Starting a Method Using System.Threading.Thread using System; using System.Threading; public class RunningASeparateThread From the Library of Wow! eBook 739 M ultithreading before .NET Framework 4 { public const int Repetitions = 1000; public static void Main() { ThreadStart threadStart = DoWork; Thread thread = new Thread(threadStart); thread.Start(); for (int count = 0; count < Repetitions; count++) { Console.Write(-); } thread.Join(); } public static void DoWork() { for (int count = 0; count < Repetitions; count++) { Console.Write(.); } } }OUTPUT 18.9: ................................--------------------------------------------- ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- .............................................................. ...

Tài liệu được xem nhiều: