How to use Ping.SendAsync working with datagridview?

天涯浪子 提交于 2019-12-02 09:48:10

What KAJ said. But there is a chance of mixing up results of ping requests because they are not connected to ip addresses in grid. One could not tell which host will respond first, and if there is a ping > 5ms anything can happen because currentrow is changing in between callbacks. What you need to do is to send a datagridviewrow reference to a callback. To do that, use an overload of SendAsync:

ping.SendAsync(ip, 1000, row);

And in a callback:

DataGridViewRow row = e.UserState as DataGridViewRow;

You might also want to check reply.Status to make sure that request did not time-out.

this refers to the current instance. A static method is not against an instance and instead just the type. So there is no this available.

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

You may also need to marshal the code back to the UI thread before trying to update the data grid view - if so then you'd need code something like the following:

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;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!