private void BtnStart_OnClick(object sender, RoutedEventArgs e) { if (_ping == null) { Debug.Assert(_plotterChildren == null); Debug.Assert(_ds == null); try { var host = (tbHost.Text ?? string.Empty).Trim(); if (string.IsNullOrWhiteSpace(host)) { throw new ArgumentException("Host can not be empty."); } var interval = TimeSpan.Parse((tbInterval.Text ?? string.Empty)); if (interval <= TimeSpan.Zero) { throw new ArgumentException("Interval must be greater than zero."); } var timeout = TimeSpan.Parse((tbTimeout.Text ?? string.Empty)); if (timeout <= TimeSpan.Zero) { throw new ArgumentException("Timeout must be greater than zero."); } _ping = new PingClient(host, interval, timeout); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } _ds = new ObservableDataSource<PingResult>(); _ping.PingTick += PingOnPingTick; //_ping.PingException += PingOnPingException; _ds.SetXMapping(item => dateAxis.ConvertToDouble(item.DateTime)); _ds.SetYMapping(item => item.TimeCost.TotalMilliseconds); _plotterChildren = new IPlotterElement[] { new LineGraph(_ds) { LinePen = new Pen(new SolidColorBrush(Colors.Green), 2), Description = new PenDescription(_ping.Host) }, new MarkerPointsGraph(_ds) {Marker = new CirclePointMarker {Size = 5}} }; foreach (var child in _plotterChildren) { plotter.AddChild(child); } Header.Content = string.Format("Ping {0}", _ping.Host); } _ping.Start(); btnStart.IsEnabled = false; btnPause.IsEnabled = true; pnSettings.IsEnabled = false; }
private void BtnStop_OnClick(object sender, RoutedEventArgs e) { if (_ping != null) { _ping.PingTick -= PingOnPingTick; _ping.Stop(); _ping = null; Debug.Assert(_plotterChildren != null); Debug.Assert(_ds != null); foreach (var child in _plotterChildren) { plotter.Children.Remove(child); } _plotterChildren = null; _ds = null; } btnStart.IsEnabled = true; btnPause.IsEnabled = false; pnSettings.IsEnabled = true; }