如何使用 Ping.SendAsync 与 datagridview 一起工作? [英] How to use Ping.SendAsync working with datagridview?

查看:25
本文介绍了如何使用 Ping.SendAsync 与 datagridview 一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序可以 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; 来获取当前行但我得到一个错误 Keyword 'this' is not available in static function.so,如何推送值回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 任何事情都可能发生,因为 currentrow 在回调之间发生变化.您需要做的是将 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;

您可能还想检查 reply.Status 以确保该请求没有超时.

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

这篇关于如何使用 Ping.SendAsync 与 datagridview 一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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