0

I have tons of WCF service calls. Simply:

foreach (var node in tree.Nodes) {
    // some preparing code here
    ....

    treeView.TocStorageInstance.UpdateNode(node) //network call
}

I'd like to parallel all these network calls which are executing one by one - as I see in the Fiddler.

Trying the next approach is not successful:

    var nodesToUpdate = new List<Task>();
    foreach (var node in tree.Nodes) {
        // some preparing code here
        ....
        nodesToUpdate.Add(await Task.Run(() => treeView.TocStorageInstance.UpdateNode(node)));
    }

    await Task.WhenAll(nodesToUpdate);

All calls still execute one-by-one in foreach statement.

Is it possible at all?

rock_walker
  • 453
  • 5
  • 14
  • 2
    you don't need await in this case. Just add Task (w/o await) to the list and Task.WhenAll(nodesToUpdate) – Leonid Malyshev Dec 13 '17 at 14:34
  • @Leonid. The same I did, but during debugging I see, that async code executes before Task.WhenAll(). And finally Task.WhenAll() completes with last item in iterator. – rock_walker Dec 13 '17 at 15:07
  • In you code you 1st wait for task to complete (await) and only after it you add that task to the list. So no parallel work. – Leonid Malyshev Dec 14 '17 at 06:56
  • In this example you can just build the tasks with linq and await them in one line... – Sievajet Dec 17 '17 at 21:23

0 Answers0