微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#Windows Forms ping.Send与DataGridView的Async问题

所以我在下面有这个代码.我发现了一个预览帖,并从这里开始工作.但由于某种原因,它不会循环并更新具有回复状态的单元格.它只更新列表中的最后一个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 /行?

我正在使用的更新代码

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.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,row);

                System.Threading.Thread.Sleep(5);
            }
        }
    }

解决方法

我认为问题是你有一个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,row);

            System.Threading.Thread.Sleep(5);
        }
    }
}

另外,我不熟悉“Ping”,但你可能想知道它是否需要被处理掉,或者把它放在Using循环中.

此外,无需将行强制转换为行.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐