If GetAllRouterInterfaces
is an async
method, the resulting Task
will already be started (see this answer for further explanation).
This means that tasks
will contain multiple tasks all of which are running in parallel without the subsequent call to Parallel.ForEach
.
You may wish to wait for all the entries in tasks
to complete, you can do this with an await Task.WhenAll(tasks);
.
So you should end up with:
var tasks = new Task<MyReturnType>[mbis.Length];
for (int i = 0; i < tasks.Length; i++)
{
tasks[i] = CAS.Service.GetAllRouterInterfaces(mbis[i], 3);
}
await Task.WhenAll(tasks);
Update from comments
It seems that despite GetAllRouterInterfaces
being async
and returning a Task
it is still making synchronous POST requests (presumably before any other await
). This would explain why you are getting minimal concurrency as each call to GetAllRouterInterfaces
is blocking while this request is made. The ideal solution would be to make an aynchronous POST request, e.g:
await webclient.PostAsync(request).ConfigureAwait(false);
This will ensure your for
loop is not blocked and the requests are made concurrently.
Further update after conversation
It seems you are unable to make the POST requests asynchronous and GetAllRouterInterfaces
does not actually do any asynchronous work, due to this I have advised the following:
- Remove
async
from GetAllRouterInterfaces
and change the return type to MyReturnType
Call GetAllRouterInterfaces
in parallel like so
var routerInterfaces = mbis.AsParallel()
.Select(mbi => CAS.Service.GetAllRouterInterfaces(mbi, 3));