如何在数据网格视图中使用Ping.SendAsync? [英] How to use Ping.SendAsync working with datagridview?

查看:124
本文介绍了如何在数据网格视图中使用Ping.SendAsync?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序ping datagridview中的每个IP,以编译响应IP RoundtripTime的列表.完成此步骤后,我会将RoundtripTime推回到datagridview.

I have an application that pings every IP in the datagridview in order to compile a list of responsive IP RoundtripTime.When finished the step,I will push the RoundtripTime back to 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;
    }

当我想使用DataGridViewRow row = this.current_row;来获取当前行但我只得到一个错误时,关键字'this'在静态函数中不可用.那么,如何将值推回datagridview?

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

谢谢.

推荐答案

KAJ所说的话.但是有可能混淆ping请求的结果,因为它们未连接到网格中的ip地址.人们无法判断哪个主机将首先响应,并且如果ping> 5ms,则可能发生任何事情,因为在回调之间电流行发生了变化.您需要做的是向回调发送一个datagridviewrow引用.为此,请使用SendAsync的重载:

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);

在回调中:

DataGridViewRow row = e.UserState as DataGridViewRow;

您可能还希望检查回复状态,以确保该请求没有超时.

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

这篇关于如何在数据网格视图中使用Ping.SendAsync?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆