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

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 に答える