为什么不Ping.Send在for循环中工作?

时间:2017-08-16 00:26:08

标签: c# .net for-loop asynchronous ping

for循环中的内容可以自行运行,但是当前循环运行后,PingTimes -list始终为空。这里发生了什么?这是C#的新手。

        private void PingTest()
    {
        List<long> PingTimes = new List<long>();
        Ping PingSender = new Ping();
        PingOptions options = new PingOptions();

        options.DontFragment = true;

        string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        byte[] buffer = Encoding.ASCII.GetBytes(data);
        int timeout = 120;


        for (int i = 1; i >= TimesToPing; i++)
        { 
            PingReply reply = PingSender.Send(PingAddress, timeout, buffer, options);
            if (reply.Status == IPStatus.Success)
            {
                PingTimes.Add(reply.RoundtripTime);
            }
            else
            {
                MessageBox.Show("Ping test failed due to an unknown reason.");
                break;
            }
        }
        MessageBox.Show("Pinged Address: " + PingAddress + "\nAverage Ping: " + PingTimes.Average().ToString() + "ms");
    }

1 个答案:

答案 0 :(得分:4)

您的for循环条件被反转,因此循环将永远不会运行。 :)