Untitled


SUBMITTED BY: Guest

DATE: Dec. 11, 2013, 6:10 a.m.

FORMAT: Text only

SIZE: 2.2 kB

HITS: 809

  1. int total = result.paginationOutput.totalPages;
  2. for (int i = 2; i < total + 1; i++)
  3. {
  4. await Task.Factory.StartNew(() =>
  5. {
  6. result = client.findItemsByProduct(i);
  7. });
  8. newList.AddRange(result.searchResult.item);
  9. }
  10. }
  11. return newList;
  12. forEach item {
  13. result = item.makeWebRequest();
  14. }
  15. foreach item {
  16. List.addRange(item.harvestResults);
  17. }
  18. var results = new ConcurrentBag<ItemType>(result.pagination.totalPages);
  19. using (var e = new CountdownEvent(result.pagination.totalPages))
  20. {
  21. for (int i = 2; i <= result.pagination.totalPages+1; i++)
  22. {
  23. Task.Factory.StartNew(() => return client.findItemsByProduct(i))
  24. .ContinueWith(items => {
  25. results.AddRange(items);
  26. e.Signal(); // signal task is done
  27. });
  28. }
  29. // Wait for all requests to complete
  30. e.Wait();
  31. }
  32. // Process results
  33. foreach (var item in results)
  34. {
  35. ...
  36. }
  37. public static Task<Item[]> Foo()
  38. {
  39. int total = result.paginationOutput.totalPages;
  40. var tasks = new List<Task<Item>>();
  41. for (int i = 2; i < total + 1; i++)
  42. {
  43. tasks.Add(Task.Factory.StartNew(() => client.findItemsByProduct(i)));
  44. }
  45. return Task.WhenAll(tasks);
  46. }
  47. int total = result.paginationOutput.totalPages;
  48. // Start all downloads; each download is represented by a task.
  49. Task<Item[]>[] tasks = Enumerable.Range(2, total - 1)
  50. .Select(i => client.findItemsByProductAsync(i)).ToArray();
  51. // Wait for all downloads to complete.
  52. Item[][] results = await Task.WhenAll(tasks);
  53. // Flatten the results into a single collection.
  54. return results.SelectMany(x => x).ToArray();

comments powered by Disqus