Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I just can't seem to understand how to structure an asynchronous call to SendPingAsync. I want to loop through a list of IP addresses and ping them all asynchronously before moving on in the program... right now it takes forever to go through all of them one at a time. I asked a question about it earlier thinking I'd be able to figure out async but apparently I was wrong.

private void button1_Click(object sender, EventArgs e)
{
    this.PingLoop();
    MessageBox.Show("hi"); //for testing

}

public async void PingLoop()
{
    Task<int> longRunningTask = PingAsync();

    int result = await longRunningTask;
    MessageBox.Show("async call is finished!"); 

    //eventually want to loop here but for now just want to understand how this works
}

private async Task<int> PingAsync()
{
    Ping pingSender = new Ping();
    string reply = pingSender.SendPingAsync("www.google.com", 2000).ToString();
    pingReplies.Add(reply); //what should i be awaiting here?? 
    return 1;
}

I'm afraid I just don't get what is really going on here enough... when should I return a task? When I run this as is I just get a frozen UI and a ping error. I have read the MSDN documentation and tons of questions here and I'm just not getting it.

like image 699
eddie_cat Avatar asked Feb 27 '14 19:02

eddie_cat


People also ask

How to ping multiple devices at the same time?

To ping from one device to multiple devices, select Tools > Diagnostics > Ping All Devices From Device.... Move your mouse to the topology map and the cursor will become a crosshair. Click on the desired source device.

What is asynchronous ping?

Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message to the computer that has the specified IPAddress, and receive a corresponding ICMP echo reply message from that computer. This overload allows you to specify a time-out value for the operation. SendAsync(IPAddress, Object)


3 Answers

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.

like image 106
Reed Copsey Avatar answered Oct 21 '22 07:10

Reed Copsey


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)

like image 33
L.B Avatar answered Oct 21 '22 06:10

L.B


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.

like image 45
usr Avatar answered Oct 21 '22 07:10

usr