0
public void ReprocessComms()
{
    List<Customer> custList = new List<Customer>();
    custList.ForEach(t => _repo.Process(t));
}

public void Process(Customer obj)
{
    Task.Factory.StartNew(() => SendComms(obj));
}

Currently I'm using this code, but I want to divide it into 4 batches and each batch should run in parallel. Each batch calls SendComms for 2500 customers.

myWorld
  • 49
  • 1
  • 11

2 Answers2

2

On the one hand, you can limit the number of tasks that is run in parallel by using ParallelOptions.MaxDegreeOfParallelism:

Parallel.ForEach(custList, 
    new ParallelOptions() { MaxDegreeOfParallelism = 4 }, 
    Process);

If you need the batches for another reason, you can split your customers in groups like this:

var groups = from custWithIndex in items.Select((x, i) => new { Index = i, Customer = x }) 
        group custWithIndex by custWithIndex.Index % 4 into grp 
        select new { GroupKey = grp.Key, Customers = grp.Select(y => y.Customer)};

On the other hand, calling Parallel.ForEach on the groups is a bit more complicated as in the previous approach, so MaxDegreeOfParallelism should be the better choice if the main goal is to limit the number of tasks that are run in parallel:

Parallel.ForEach(groups, group => {
    foreach(var cust in group.Customers)
        Process(cust);
    });
Markus
  • 20,838
  • 4
  • 31
  • 55
  • I have added the ' public void ReprocessComms() { List custList = new List(); // custList.ForEach(t => _repo.Process(t)); Parallel.ForEach(custList, new ParallelOptions { MaxDegreeOfParallelism = 5 }, (obj) => { _repo.SendComms(obj); }); }' – myWorld Feb 11 '16 at 21:52
-1
I thought we can use linq take() and skip()  methods. But we need to specify the starting and ending index. 

Total 10000,
List<Customerlist>CustomerlistBatch=new List<customerlist>();
Dictionary<int,int> StartEnd=new Dictionary<int,int>();
0  = > 0-2500
1  = > 2500-5000
2  = > 5000-7500
3  = > 7500-10000

For each item as StartEnd
{
CustomerlistBatch.Add(customerlist.take(item.key).skip(item.value).Select(x=>x).tolist());

}
  • i dont want to resctict like 2500 for each batch. total number of customers can change as it comes from UI. text box for number of customers. person using the UI can input 5k or 10k or 20k in the UI – myWorld Feb 11 '16 at 22:00