C # Windows Forms ping.SendAsync problems with DataGridView

So, I have this code below. I found a preliminary post and worked from here. But for some reason, it does not iterate over or update the cells with the response. It updates only the last ip in the list.

private static void ping_PingCompleted(object sender, PingCompletedEventArgs e) { var reply = e.Reply; DataGridViewRow row = e.UserState as DataGridViewRow; DataGridViewCell PingStat = row.Cells["cPing"]; if (!(reply == null)) { switch (reply.Status) { case IPStatus.Success: PingStat.Value = string.Format("Reply from {0}: bytes={1} time={2}ms TTL={3}", reply.Address, reply.Buffer.Length, reply.RoundtripTime, reply.Options.Ttl); break; case IPStatus.TimedOut: PingStat.Value = "Connection has timed out..."; break; default: PingStat.Value = string.Format("Ping failed: {0}", reply.Status.ToString()); break; } } } private void bPing_Click(object sender, EventArgs e) { String ip; Ping ping = new Ping(); foreach (DataGridViewRow row in dgvData.Rows) { if(!row.IsNewRow) { ip = row.Cells["cIPAddress"].Value.ToString(); ping.PingCompleted += new PingCompletedEventHandler(ping_PingCompleted); ping.SendAsync(ip, 1000, row); System.Threading.Thread.Sleep(5); } } } 

What am I doing wrong? I thought adding a line to ping.SendAsync, will it track all responses to the corresponding ip / row?

UPDATED Code I'm working with

  private static void ping_PingCompleted(object sender, PingCompletedEventArgs e) { var reply = e.Reply; DataGridViewRow row = e.UserState as DataGridViewRow; DataGridViewCell PingStat = row.Cells["cPing"]; if (reply != null) { switch (reply.Status) { case IPStatus.Success: PingStat.Value = string.Format("Reply from {0}: bytes={1} time={2}ms TTL={3}", reply.Address, reply.Buffer.Length, reply.RoundtripTime, reply.Options.Ttl); break; case IPStatus.TimedOut: PingStat.Value = "Connection has timed out..."; break; default: PingStat.Value = string.Format("Ping failed: {0}", reply.Status.ToString()); break; } } } private void bPing_Click(object sender, EventArgs e) { foreach (DataGridViewRow row in dgvData.Rows) { if (!row.IsNewRow) { Debug.WriteLine("Rows"); String ip; Ping ping = new Ping(); ip = row.Cells["cIPAddress"].Value.ToString(); ping.PingCompleted += new PingCompletedEventHandler(ping_PingCompleted); ping.SendAsync(ip, 1000, row); System.Threading.Thread.Sleep(5); } } } 
+4
source share
2 answers

I think the problem is that you have one Ping and one IP, and they keep getting reset to the last line. If you move these vars to the foreach loop, each row in the DataGridView will have its own β€œPing” and ip, and therefore you will not have a problem with each row effectively destroying the previous ones.

 private void bPing_Click(object sender, EventArgs e) { foreach (DataGridViewRow row in dgvData.Rows) { if(!row.IsNewRow) { String ip; Ping ping = new Ping(); ip = row.Cells["cIPAddress"].Value.ToString(); ping.PingCompleted += new PingCompletedEventHandler(ping_PingCompleted); ping.SendAsync(ip, 1000, row); System.Threading.Thread.Sleep(5); } } } 

In addition, I am not familiar with "Ping", but you can see if you need to delete it or put it in the usage cycle.

There is also no need to throw a string into a string.

+1
source

How about this:

 private void bPing_Click(object sender, EventArgs e) { foreach (DataGridViewRow row in dgvData.Rows) { try { Ping pinger = new Ping(); PingReply reply = await pinger.SendPingAsync(row.Cells["cIPAddress"].Value.ToString(),1000); switch (reply.Status) { // do your stuff here } } catch (PingException) { // Handle exception here } } } 
0
source

All Articles