Question

I'm trying to run a few async tasks concurrently inside my Portable Class Library, but the WhenAll method doesn't appear to be supported.

My current workaround is to start each task and then await each of them:

var task1 = myService.GetData(source1);
var task2 = myService.GetData(source2);
var task3 = myService.GetData(source3);

// Now everything's started, we can await them
var result1 = await task1;
var result1 = await task2;
var result1 = await task3;

Is there something I'm missing? Do I need to make do with the workaround?

Was it helpful?

Solution

Is there something I'm missing?

Yes: Microsoft.Bcl.Async.

Once you install that package, then you can use TaskEx.WhenAll as a substitute for Task.WhenAll:

var task1 = myService.GetData(source1);
var task2 = myService.GetData(source2);
var task3 = myService.GetData(source3);

// Now everything's started, we can await them
var results = await TaskEx.WhenAll(task1, task2, task3);

P.S. Consider using the term parallel for (CPU-bound) parallel processing, and the term concurrent for doing more than one thing at a time. In this case, the (I/O-bound) tasks are concurrent but not parallel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top