How to use Ping.SendAsync working with datagridview?

I have an application that records every IP address in a datagridview to compile a list of responsive RoundTripTime IP events. After completing this step, I will return to RoundtripTime back in the datagridview.

... foreach (DataGridViewRow row in this.gvServersList.Rows) { this.current_row = row; string ip = row.Cells["ipaddr_hide"].Value.ToString(); ping = new Ping(); ping.PingCompleted += new PingCompletedEventHandler(ping_PingCompleted); ping.SendAsync(ip, 1000); System.Threading.Thread.Sleep(5); } ... private static void ping_PingCompleted(object sender, PingCompletedEventArgs e) { var reply = e.Reply; DataGridViewRow row = this.current_row; //notice here DataGridViewCell speed_cell = row.Cells["speed"]; speed_cell.Value = reply.RoundtripTime; } 

When I want to use DataGridViewRow row = this.current_row; to get the current row, but I just get the error message The keyword 'this' is not available in static function.so, how to return the value back to datagridview?

Thanks.

0
source share
2 answers

What KAJ said. But there is a chance to mix the results of ping requests because they are not related to the IP addresses in the grid. It is not possible to determine which host will respond first, and if there is ping> 5ms, everything can happen because the current change changes between callbacks. What you need to do is send the datagridviewrow link to the callback. To do this, use the SendAsync overload:

 ping.SendAsync(ip, 1000, row); 

And in the callback:

 DataGridViewRow row = e.UserState as DataGridViewRow; 

You can also check answer.Status to make sure the request is not disconnected.

0
source

this refers to the current instance. The static method is not against the instance, but simply of type. So there is no this .

Therefore, you need to remove the static from the event handler declaration. Then the method will be against the instance.

You may also need to translate the code back into the user interface stream before trying to update the data grid view - if so, you will need the code something like this:

 delegate void UpdateGridThreadHandler(Reply reply); private void ping_PingCompleted(object sender, PingCompletedEventArgs e) { UpdateGridWithReply(e.Reply); } private void UpdateGridWithReply(Reply reply) { if (dataGridView1.InvokeRequired) { UpdateGridThreadHandler handler = UpdateGridWithReply; dataGridView1.BeginInvoke(handler, table); } else { DataGridViewRow row = this.current_row; DataGridViewCell speed_cell = row.Cells["speed"]; speed_cell.Value = reply.RoundtripTime; } } 
0
source

All Articles