I’ve got the follow code, I just need to make sure this is the correct way to do this.. It works and everything but isn’t as quick as Id expect.. I’ve timed each individual call and the longest is no where near the time it takes it takes to run.
public async Task<Result[]> DoSomethingGoodAsync()
{
List<Product> productList = getproducts();
IEnumerable<Task<Result>> list =
from p in productList select DoSomethingAsync(p);
Task<Result>[] slist = list.ToArray();
return await Task.WhenAll(slist);
}
Now my question again, is this correct? Is there a better and more efficient way to do this? DoSomethingAsync is an awaitable method also which calls another async method.
Edit: My question.. Is this the correct way to build up a collection of awaitable methods that I want to execute together?
Inside DoSomethingAysnc()
scrapeResult = await UrlScraper.ScrapeAsync(product.ProductUrl);
model = this.ProcessCheckStock(model, scrapeResult, product);
It appears that
getproductsreturns a type assignable toIList<T>. This means thatgetproductsmaterializes the result set before you callDoSomethingAsyncon each item returned fromgetproducts.Depending on how long it takes to yield each item and materialize the set is more than likely having the most impact.
That said, you should change the
getproductsmethod to return anIEnumerable<T>implementation. But you need to do more than change the return type, you need to remove the materialization (more than likely a call toToList) and useyieldinstead.