MicroMan
MicroMan

Reputation: 2098

Set DegreeofParallelism when using AsOrdered c#

I get the following error "AsOrdered may only be called on the result of AsParallel, ParallelEnumerable.Range, or ParallelEnumerable.Repeat"

When running the following code

myListofActions.AsParallel().WithDegreeOfParallelism(threadCount)
                                        .AsOrdered().ForAll(x => DoMyTask(x));

this code works fine

myListofActions.AsParallel().AsOrdered().ForAll(x => DoMyTask(x));

Is there a way to set the WithDegreeOfParallelism in this case?

Thanks in advance

Upvotes: 2

Views: 481

Answers (1)

Rufus L
Rufus L

Reputation: 37030

Try this:

myListofActions.AsParallel()
        .AsOrdered()
        .WithDegreeOfParallelism(Math.Min(threadCount, Environment.ProcessorCount))
        .ForAll(DoMyTask);

Upvotes: 4

Related Questions