4

所以我在下面有这段代码。我找到了一个预览帖子,并从这里开始工作。但由于某种原因,它没有循环遍历并使用回复状态更新单元格。它只更新列表中的最后一个 ip。

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

我在做什么不正确?我认为向 ping.SendAsync 添加行会跟踪对相应 ip/row 的所有回复?

我正在使用的更新代码

        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

2 回答 2

1

我认为问题在于您有一个 Ping 和一个 IP,并且这些一直重置到最后一行。如果将这些变量移动到 foreach 循环中,则 DataGridView 中的每一行都将有其“自己的”Ping 和 ip,因此您不会遇到每行有效撤消前一行的问题。

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

另外,我不熟悉“Ping”,但您可能想看看它是否需要处理,或者将其放入 Using 循环中。

也不需要将一行转换为一行。

于 2015-08-07T05:36:46.307 回答
0

那这个呢:

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
        }
    }
}
于 2015-08-07T08:49:16.253 回答