TAILIEUCHUNG - Addison Essential Csharp_8

Chạy LINQ truy vấn trong song song 18,17: Hủy một vòng lặp song song bằng cách sử dụng sử dụng sử dụng sử dụng sử dụng hệ thống; | Running LINQ Queries in Parallel 737 Listing Canceling a Parallel Loop using System using using using using public class Program public static List string ParallelEncrypt List string data CancellationToken cancellationToken return .WithCancellation cancellationToken .Select item Encrypt item .ToList public static void Main List string data 1000000 .ToList CancellationTokenSource cts new CancellationTokenSource Push ENTER to exit. Task task data ParallelEncrypt data Wait for the user s input stars try catch AggregateException . Output ERROR The operation was canceled. Chapter 18 Multithreading As with a parallel loop canceling a PLINQ query requires a CancellationToken which is available on a property. However rather than overloading every PLINQ query to support the cancellation token the ParallelQuery T object returned by lEnumera-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 queries will need to be wrapped by try . catch OperationCanceledExcep-tion . blocks to avoid an unhandled exception. Alternatively as shown in Listing pass the CancellationToken to both ParallelEncrypt and as a second parameter on StartNew . This will cause 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 .

TỪ KHÓA LIÊN QUAN
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.