for-loop內部的內容可以正常工作,但是在循環運行後,PingTimes -list始終爲空。這裏發生了什麼?這裏對於C#來說很新穎。Ping.Send爲什麼不在for循環中工作?
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");
}
在計算機科學碩士真的應該知道如何使用_debugger_ – MickyD