How to pass different range on parallel.for?

496 Views Asked by At

I need to process the single file in parallel by sending skip-take count like 1-1000, 1001-2000,2001-3000 etc

Code for parallel process

var line = File.ReadAllLines("D:\\OUTPUT.CSV").Length;
Parallel.For(1, line, new ParallelOptions { MaxDegreeOfParallelism = 10 }, x              
=> {
  DoSomething(skip,take);
});

Function

public static void DoSomething(int skip, int take)
{
     //code here
}

How can send the skip and take count in parallel process as per my requirement ?

1

There are 1 best solutions below

8
Yuval Itzchakov On BEST ANSWER

You can do these rather easily with PLINQ. If you want batches of 1000, you can do:

const int BatchSize = 1000;

var pageAmount = (int) Math.Ceiling(((float)lines / BatchSize));
var results = Enumerable.Range(0, pageAmount)
                        .AsParallel()
                        .Select(page => DoSomething(page));

public void DoSomething(int page)
{
    var currentLines = source.Skip(page * BatchSize).Take(BatchSize);
    // do something with the selected lines
}