I have an asynchronous
async static public Task<int> Test(int x, int y)
{
await Task.Delay(1000);
return x;
}
And I try to run it in parallele on a list. I did like that:
var x = new[] { 0, 1 };
var y = new[] { 2, 3 };
var zipped = x.Zip(y, (x, y) => new { x, y }).ToList();
zipped.AsParallel().ForAll(async pair => { await Test(pair.x, pair.y); });
But I don't manage to retrieve the list it should return [0,1] cause the AsParallel returns void
Any idea ? also, if there is better solution to speed up in true parralel programming, I take
If
AsParallelwould have returnedvoidthen you would not be able to callForAllon it's result. It isForAllwhich hasvoidreturn type.If you want to get the result - use
Select:BUT PLINQ is not async/Task-aware (as you can somewhat see by the return type) so if your
Testis purely IO-bound it does not make much sense here since it will just start all the tasks without actually limiting number of tasks scheduled in parallel (i.e. only task creation is parallelized). Hence it would be similar to just using ordinary LINQ:If you want to limit concurrently "scheduled"/running tasks you can use
SemaphoreSlimorParallel.ForEachAsync. For example:See also: