Thursday, 15 January 2015

c# - Getting return values from Task.WhenAll -



c# - Getting return values from Task.WhenAll -

hopefully simple 1 here. have collection of objects, each of has async method want phone call , collect values from. i'd them run in parallel. i'd accomplish can summed in 1 broken line of code:

ienumerable<testresult> results = await task.whenall(mycollection.select(v => v.testasync()));

i've tried various ways of writing without success. thoughts?

if tasks you're awaiting have result of same type task.whenall returns array of them. illustration class:

public class test { public async task<testresult> testasync() { await task.delay(1000); // imagine i/o operation. homecoming new testresult(); } }

we these results:

var mycollection = new list<test>(); mycollection.add(new test()); ienumerable<testresult> results = await task.whenall(mycollection.select(v => v.testasync()));

c# task-parallel-library async-await

No comments:

Post a Comment