Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AsParallel() - with more than 2 threads in parallel in asp.net

I have a method that I call 8 times with different parametres. I use

 AvailableYears.AsParallel()
             .Select<Int32,DateUsedByThread>(x => GetDataForYearWorker(x,CIF))
             .ToList();

GetDataForYearWorker gets the response from a webservice synchronously. It uses very little computing power on my asp.net application, but it ussualy takes 3-5 sec for each webservice response. Because the calls to the webservice are independent of eachother, I want to make tham all at the same time. But it looks like only 2 threads can run at the same time. Why is this and how can I have 8 threads working at the same time?

like image 572
Ryan Avatar asked Feb 21 '11 13:02

Ryan


1 Answers

By default .AsParallel() will spin up one thread per core on the machine running the query. If you wish to alter this behavior look at WithDegreeOfParallelism.

AvailableYears.AsParallel().WithDegreeOfParallelism(5)
             .Select<Int32,DateUsedByThread>(x => GetDataForYearWorker(x,CIF))
             .ToList();
like image 200
Mark Coleman Avatar answered Nov 15 '22 18:11

Mark Coleman