Thor Burfine Thor Burfine - 4 months ago 25
C# Question

Threading in blocks

I have a list of IDs , lets say 1,000 of them.

I need to start a thread for each one with a max of 10 running at any time

I thought of using paging on a Linq result set but I would loose the running of 10 all the time.

Any suggestions on how I can keep 10 threads running at all times until all records are processed

Answer Source

I suggest using Parallel Linq (PLinq), something like this:

List<int> IDs = ...

IDs
  .AsParallel()
  .WithDegreeOfParallelism(10)
  .ForAll(id => /*TODO: put relevant code here*/ );