Send multiple asynchronous web requests and compile all responses

122 Views Asked by At

I am trying to loop through a set of data and send a web request for each item in the list. I want to send each web request asynchronously and continue to send the remaining requests.

The tricky thing is I want to capture and compile the responses from all of these requests.

I'm just unsure of how exactly the task factory works and if my solution is really doing what I want it to do.

Here is what I have in my for loop:

var runningTasks = new List<Task<string>>();
var taskResp = Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);
var taskResult = taskResp.ContinueWith(t => new StreamReader(t.Result.GetResponseStream()).ReadToEnd().Trim());
runningTasks.Add(taskResult);

Task.WaitAll(runningTasks.ToArray());
IEnumerable<string> results = runningTasks.Select(t => t.Result);
0

There are 0 best solutions below