I'm building a WP8 app and I need to perform around 30 web requests. These requests don't depend on each other so they could be parallelized.
My code looks like this (simplified/pseudocode):
foreach (Uri uri in uris)
{
var rawData = await Task.Run(() => httpClient.GetStringAsync(uri).ConfigureAwait(false));
var processedData = dataProcessor.Process(rawData);
processedDataCollection.Add(processedData);
}
When I look at Fiddler, the requests are all performed sequantial; it takes a few seconds to perform and process all of them. However, I don't want the code to wait until 1 request is finished before moving to the next one, I want to perform multiple requests at the same time.
Normally I would use Parallel.Invoke()
or Parallel.ForEach()
or something like that to do this, but apparently the Parallel library is not available in Windows Phone 8.
So what's the best way to accomplish this? Task.Factory.StartNew()
? new Thread()
?
There's no need to run each request on a separate thread. You can just as easily do this:
var raw = await Task.WhenAll(uris.Select(uri => httpClient.GetStringAsync(uri)));
var processed = raw.Select(data => dataProcessor.Process(data)).ToArray();
This code takes the collection of uris
and starts an HTTP download for each one (Select(...)
). Then it asynchronously waits for them all to complete (WhenAll
). All the data is then processed in the second line of code.
However, it's likely that the Windows Phone runtime will limit the maximum number of requests you can have to the same server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With