private void Ping_PingReceived(object sender, PingReceivedArgs e) { PingInfo pingInfo = PingInfo.Parse(e); // Add the result to the collection Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate() { lock (PingResult) PingResult.Add(pingInfo); })); // Calculate statistics PingsTransmitted++; if (pingInfo.Status == System.Net.NetworkInformation.IPStatus.Success) { PingsReceived++; if (PingsReceived == 1) { MinimumTime = pingInfo.Time; MaximumTime = pingInfo.Time; } else { if (MinimumTime > pingInfo.Time) { MinimumTime = pingInfo.Time; } if (MaximumTime < pingInfo.Time) { MaximumTime = pingInfo.Time; } // lock, because the collection is changed from another thread... // I hope this won't slow the application or causes a hight cpu load lock (PingResult) AverageTime = (int)PingResult.Average(s => s.Time); } } else { PingsLost++; } }
private void Ping_PingReceived(object sender, PingReceivedArgs e) { PingInfo pingInfo = PingInfo.Parse(e); // Add the result to the collection PingResult.Add(pingInfo); // Calculate statistics PingsTransmitted++; if (pingInfo.Status == System.Net.NetworkInformation.IPStatus.Success) { PingsReceived++; if (PingsReceived == 1) { MinimumTime = pingInfo.Time; MaximumTime = pingInfo.Time; } else { if (MinimumTime > pingInfo.Time) { MinimumTime = pingInfo.Time; } if (MaximumTime < pingInfo.Time) { MaximumTime = pingInfo.Time; } } // I don't know if this can slow my application if the collection is to large AverageTime = (int)PingResult.Average(s => s.Time); } else { PingsLost++; } }
private void ScanNeighbors() { while (true) { var from = new IPEndPoint(IPAddress.Any, 0); var buffer = _client.Receive(ref from); var header = Encoding.UTF8.GetString(buffer, 0, buffer.Length); Debugger.Log("Received broadcast header: " + header); var pingInfo = PingInfo.Parse(header); if (pingInfo == null || pingInfo.Id == _id) { continue; } var address = from.Address.ToString(); var neighbor = new Neighbor { Name = pingInfo.Name, LastOnline = DateTime.Now, Address = address }; lock (this) { var oldSize = _neighbors.Count; _neighbors[address] = neighbor; if (oldSize != _neighbors.Count) { OnNeighborsChanged?.Invoke(); } } } }