How can I make many pings asynchronously at the same time?

What you do here pingSender.SendPingAsync("www.google.com", 2000).ToString(); doesn't make much sense.

Instead you should return pingSender.SendPingAsync("www.google.com", 2000) and

await Task.WhenAll(your all ping requests)


What you want is to start all pings at once:

var pingTargetHosts = ...; //fill this in
var pingTasks = pingTargetHosts.Select(
     host => new Ping().SendPingAsync(host, 2000)).ToList();

Now the pings are running. Collect their results:

var pingResults = await Task.WhenAll(pingTasks);

Now the concurrent phase of the processing is done and you can examine and process the results.


You'd want to do something like:

private async Task<List<PingReply>> PingAsync()
{
    var tasks = theListOfIPs.Select(ip => new Ping().SendPingAsync(ip, 2000));
    var results = await Task.WhenAll(tasks);

    return results.ToList();
}

This will start off one request per IP in theListOfIPs asynchronously, then asynchronously wait for them all to complete. It will then return the list of replies.

Note that it's almost always better to return the results vs. setting them in a field, as well. The latter can lead to bugs if you go to use the field (pingReplies) before the asynchronous operation completes - by returning, and adding the range to your collection after the call is made with await, you make the code more clear and less bug prone.