private void OnPingCompleted(object _, PingCompletedEventArgs args) { if (args.Cancelled) { return; } if (args.Error != null) { TriggerPingFailure(IPStatus.Unknown); return; } PingReply reply = args.Reply; if (reply.Status != IPStatus.Success) { TriggerPingFailure(reply.Status); return; } PingResponseTimes.Add(reply.RoundtripTime); if (PingResponseTimes.Count >= IndividualPings.Count) { CancelPendingPings(); long averageTime; // Must use the lock here as the extension method iterates over the collection lock (PingResponseTimes.__InternalListLock) averageTime = PingResponseTimes.GetMeanRoundedToNearestWhole(); Dispatcher.InvokeAsync(() => { // Invoke on the main thread PingSuccessCallback?.Invoke(new PingSuccess() { AverageRoundTripTimeMilliseconds = averageTime }); ClearPingResponseStuff(); }); } }