Call webservice asynchronously and wait for all threads to be finished

后端 未结 3 1541
情话喂你
情话喂你 2021-01-24 12:04

I need to call the web service several times to get data, and then put those data into my database, so I\'ve got the following code:

foreach (string v in options         


        
3条回答
  •  无人及你
    2021-01-24 12:59

    This downloads all in parallel and stores them into the DB with one write.

    var tasks = options.Select(o => Task.Run(() => GetData(o)));
    Task.WaitAll(tasks.ToArray());
    var nodes = tasks.SelectMany(t => t.Result);
    _dbService.SaveToDb(nodes);
    

提交回复
热议问题