Windows Phone, Multiple HTTP request parallel, how many?
async-await, c#, dotnet-httpclient, windows-phone-8
Solution
How can I improve this to make multiple request in parallel?
You can do the parallel processing like below (untested). It uses `SemaphoreSlim` to throttle `getDetails` requests.
async Task ProcessPlanes()
{
const int MAX_REQUESTS = 50;
List<plane> planes = await planeService.getPlanes(); // Get all planes from web api
var semaphore = new SemaphoreSlim(MAX_REQUESTS);
Func<string, Task<Details>> getDetailsAsync = async (id) =>
{
await semaphore.WaitAsync();
try
{
var details = await planeService.getDetails(id);
drawDetails(details);
return details;
}
finally
{
semaphore.Release();
}
};
var tasks = planes.Select((plane) =>
getDetailsAsync(plane.id));
await Task.WhenAll(tasks.ToArray());
}
what is resonable number of request running parallel? The planes list can be anything from 0 to 100 objects, typically max 20.
It largely dependents on the server, but I don't think there's an ultimate answer to this. For example, check this question:
A reasonable number of simultaneous, asynchronous ajax requests
As far as the WP8 client goes, I believe it can spawn 100 parallel requests without a problem.
Problem
In my Windows Phone 8 app, Im fetching list of items from web api. After that I loop all items and get details for each Item. Right now my code is something like this: ``` List<plane> planes = await planeService.getPlanes(); // Get all planes from web api foreach(Plane plane in planes) { var details = await planeService.getDetails(plane.id); // Get one plane details from web api drawDetails(details); } ``` How can I improve this to make multiple request in parallel and what is resonable number of request running parallel? The planes list can be anything from 0 to 100 objects, typically max 20.