我正在使用C#WPF开发一个新程序,其中一部分包括平均十PingReply.RoundTripTime
并将其除以2以找到单向行程时间。我将所有PingReply
个实例放在一个数组arrayReply
中,现在想要执行上述操作。我尝试了很多东西,但这就是我现在所拥有的:
//New integer replyCount for counting ping replies
int replyCount = 0;
long Atime = 0;
long time = 0;
//for each PingReply instance in arrayReply
foreach (PingReply r in arrayReply)
{
//set long integer (64-bit) Atime (time of ping) to the previous value of Atime
//plus half of the next RoundTripTime divided by 2 (for approx one-way distance)
long oneWayTime = r.RoundtripTime / 2;
long x = Atime + oneWayTime;
//add one to replyCount
replyCount++;
//Divide Atime by number of replies
time = x / replyCount;
}
pingAverage.Text = time.ToString();
但是,每次都会产生0。我该如何解决这个问题?
答案 0 :(得分:0)
使用LINQ进行平均:
var time = arrayReply.Average(pr => pr.RoundtripTime) / 2;