Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run parallel tasks on Windows Phone?

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()?

like image 335
Leon Cullens Avatar asked Nov 29 '13 14:11

Leon Cullens


1 Answers

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.

like image 176
Stephen Cleary Avatar answered Oct 15 '22 10:10

Stephen Cleary