private void Timer_Tick(object sender, EventArgs e) { if (_pinger is not null && _pinger.Running) { StopButtonEnabled = true; StartButtonEnabled = false; StringBuilder sb = new StringBuilder(); for (int i = 0; i < _pinger.Replies.Count - 1; i++) { _totalPings++; var success = _pinger.Replies.TryTake(out Reply reply); if (success) { if (Config.WindowExpanded) { var s = (LineSeries)GraphModel.Series[0]; var maxPoints = (Host.Interval * 30) / 1000; if (s.Points.Count > maxPoints) { s.Points.RemoveAt(0); } s.Points.Add(new DataPoint(reply.DateTime.ToOADate(), reply.RoundTrip)); Dispatcher.UIThread.InvokeAsync(() => GraphModel.InvalidatePlot(true), DispatcherPriority.Background); var p = (PieSeries)StatusModel.Series[0]; if (reply.Succeeded.Value) { if (reply.RoundTrip > Host.Threshold) { var oldValue = p.Slices[1].Value; p.Slices[1] = new PieSlice("Warning", oldValue + 1) { Fill = OxyColor.FromRgb(235, 224, 19) }; } else { var oldValue = p.Slices[0].Value; p.Slices[0] = new PieSlice("Success", oldValue + 1) { Fill = OxyColor.FromRgb(51, 204, 0) }; } } else { var oldValue = p.Slices[2].Value; p.Slices[2] = new PieSlice("Failure", oldValue + 1) { Fill = OxyColor.FromRgb(194, 16, 16) }; } Dispatcher.UIThread.InvokeAsync(() => StatusModel.InvalidatePlot(true), DispatcherPriority.Background); } Log.Debug("Ping Success"); sb.Append($"[{reply.DateTime:T}] "); if (reply.RoundTrip > 0) { _pingTimes.Add(reply.RoundTrip); Log.Debug($"{HostName} RoundTrip Time > 0: {reply.RoundTrip}"); } if (reply.TimedOut) { TimeoutCount++; Log.Debug($"{HostName} Reply timed out. Number of Timeouts: {TimeoutCount}"); sb.Append($"Timed out to host"); } else { sb.Append($"Ping round trip: {reply.RoundTrip}ms"); if (reply.RoundTrip >= WarningThreshold) { WarningCount++; sb.Append(" [Warning]"); } } sb.Append(Environment.NewLine); } } PingStatusText += sb.ToString(); var lines = PingStatusText.Split(Environment.NewLine).ToList(); if (lines.Count > _pingTimes.MaxSize) { lines.RemoveAt(0); PingStatusText = string.Join(Environment.NewLine, lines); } if (_pingTimes.Count > 0) { AveragePing = Math.Ceiling(_pingTimes.Average()).ToString() + "ms"; } PacketLoss = $"{Math.Round(TimeoutCount / (double)_totalPings * 100, 2)}%"; }
void Timer_Tick(object sender, EventArgs e) { if (Pinger != null && Pinger.Running) { if (Config.WindowExpanded) { switch (rightTabs.SelectedIndex) { case 1: pingGraphControl.UpdatePlot(); break; case 2: statusGraphControl.UpdatePieChart(PingHost.Threshold, PingHost.Timeout); break; } } StartBtn.Visibility = Visibility.Hidden; StopBtn.Visibility = Visibility.Visible; doTraceRteBtn.Visibility = Visibility.Hidden; StringBuilder sb = new StringBuilder(); for (int i = 0; i < Pinger.Replies.Count - 1; i++) { TotalPings++; // Logger.Info($"{PingHost.HostName} TotalPings: {TotalPings}"); var success = Pinger.Replies.TryTake(out Reply reply); pingGraphControl.AddData(reply.DateTime, reply.RoundTrip); statusGraphControl.AddData(reply.DateTime, reply.RoundTrip); if (success) { Logger.Debug("Ping Success"); var line = $"[{reply.DateTime:T}] "; if (reply.RoundTrip > 0) { PingTimes.Add(reply.RoundTrip); Logger.Debug($"{PingHost.HostName} RoundTrip Time > 0: {reply.RoundTrip}"); } if (reply.TimedOut) { Timeouts++; Logger.Debug($"{PingHost.HostName} Reply timed out. Number of Timeouts: {Timeouts}"); line += $"Timed out to host"; } else { line += $"Ping round trip: {reply.RoundTrip}ms"; if (reply.RoundTrip >= PingHost.Threshold) { Warnings++; line += " [Warning]"; } } sb.Append(line); sb.Append(Environment.NewLine); } } PingStatusBox.Text += sb.ToString(); var lines = PingStatusBox.Text.Split(Environment.NewLine).ToList(); if (lines.Count > PingTimes.MaxSize) { Logger.Debug($"{PingHost.HostName} Lines in text box greater than {PingTimes.MaxSize}, removing a line."); lines.RemoveAt(0); PingStatusBox.Text = string.Join(Environment.NewLine, lines); } if (PingTimes.Count > 0) { avgPingLbl.Content = Math.Ceiling(PingTimes.Average()).ToString() + "ms"; } timeoutLbl.Content = Timeouts.ToString(); warningLbl.Content = Warnings.ToString(); PacketLoss = ((double)Timeouts / (double)TotalPings) * 100; packetLossLabel.Content = $"{Math.Round(PacketLoss, 2)}%"; } else { StartBtn.Visibility = Visibility.Visible; StopBtn.Visibility = Visibility.Hidden; doTraceRteBtn.Visibility = Visibility.Visible; if (IPAddressBox.Text == "Invalid Host Name") { StartBtn.IsEnabled = false; doTraceRteBtn.IsEnabled = false; } else { StartBtn.IsEnabled = true; doTraceRteBtn.IsEnabled = true; } } CheckForFolder(); }