コード例 #1
0
        /// <summary>This is the method to opt into Parallel LINQ.</summary>
        /// <typeparam name="TSource">Specifies the type of elements provided to the query.</typeparam>
        /// <param name="source">The source query.</param>
        /// <param name="parallelOptions">The options to use for query processing.</param>
        /// <returns>The source as a ParallelQuery to bind to ParallelEnumerable extension methods.</returns>
        public static ParallelQuery <TSource> AsParallel <TSource>(this IEnumerable <TSource> source,
                                                                   ParallelLinqOptions parallelOptions)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (parallelOptions.TaskScheduler != null && parallelOptions.TaskScheduler != TaskScheduler.Default)
            {
                throw new ArgumentException("Parallel LINQ only supports the default TaskScheduler.");
            }
            ParallelQuery <TSource> source1 = source.AsParallel();

            if (parallelOptions.Ordered)
            {
                source1 = source1.AsOrdered();
            }
            if (parallelOptions.CancellationToken.CanBeCanceled)
            {
                source1 = source1.WithCancellation(parallelOptions.CancellationToken);
            }
            if (parallelOptions.MaxDegreeOfParallelism >= 1)
            {
                source1 = source1.WithDegreeOfParallelism(parallelOptions.MaxDegreeOfParallelism);
            }
            if (parallelOptions.ExecutionMode != ParallelExecutionMode.Default)
            {
                source1 = source1.WithExecutionMode(parallelOptions.ExecutionMode);
            }
            if (parallelOptions.MergeOptions != ParallelMergeOptions.Default)
            {
                source1 = source1.WithMergeOptions(parallelOptions.MergeOptions);
            }
            return(source1);
        }
コード例 #2
0
        /// <summary>This is the method to opt into Parallel LINQ.</summary>
        /// <typeparam name="TSource">Specifies the type of elements provided to the query.</typeparam>
        /// <param name="source">The source query.</param>
        /// <param name="parallelOptions">The options to use for query processing.</param>
        /// <returns>The source as a ParallelQuery to bind to ParallelEnumerable extension methods.</returns>
        public static ParallelQuery <TSource> AsParallel <TSource>(
            this IEnumerable <TSource> source,
            ParallelLinqOptions parallelOptions)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            // Validate unsupported options
            if (parallelOptions.TaskScheduler != null && parallelOptions.TaskScheduler != TaskScheduler.Default)
            {
                throw new ArgumentException("Parallel LINQ only supports the default TaskScheduler.");
            }

            // First convert to PLINQ
            var result = source.AsParallel();

            // Then apply all of the options as requested...
            if (parallelOptions.Ordered)
            {
                result = result.AsOrdered();
            }
            if (parallelOptions.CancellationToken.CanBeCanceled)
            {
                result = result.WithCancellation(parallelOptions.CancellationToken);
            }
            if (parallelOptions.MaxDegreeOfParallelism >= 1)
            {
                result = result.WithDegreeOfParallelism(parallelOptions.MaxDegreeOfParallelism);
            }
            if (parallelOptions.ExecutionMode != ParallelExecutionMode.Default)
            {
                result = result.WithExecutionMode(parallelOptions.ExecutionMode);
            }
            if (parallelOptions.MergeOptions != ParallelMergeOptions.Default)
            {
                result = result.WithMergeOptions(parallelOptions.MergeOptions);
            }
            return(result);
        }