3

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);
4

1 回答 1

1

似乎getproducts返回一个可分配给的类型IList<T>。这意味着getproducts在您调用DoSomethingAsyncgetproducts.

取决于生产每个项目和实现该集合需要多长时间,这很可能产生最大的影响。

也就是说,您应该更改getproducts方法以返回IEnumerable<T>实现。但是您需要做的不仅仅是更改返回类型,还需要删除具体化(很可能是对 的调用ToList)并yield改用它。

于 2012-10-30T13:08:02.233 回答