Пример #1
0
        private void Pinging()
        {
            int countOfPing  = 1;
            int lastPosition = Convert.ToInt32(chartCanvas.ActualHeight);

            minValue = long.MaxValue;
            maxValue = 0L;
            sum      = 0L;
            avgValue = sum / countOfPing;
            int newMargin = 4;

            while (!isAppClosing)
            {
                if (isStarted)
                {
                    PingReply reply        = Pinger.Start(Host);
                    int       canvasHeight = Convert.ToInt32(chartCanvas.ActualHeight);
                    if (minValue > reply.RoundtripTime)
                    {
                        minValue = reply.RoundtripTime;
                    }
                    if (maxValue < reply.RoundtripTime)
                    {
                        maxValue = reply.RoundtripTime;
                    }
                    sum     += reply.RoundtripTime;
                    avgValue = (double)sum / (double)countOfPing;
                    avgValue = Math.Round(avgValue, 3);

                    this.Dispatcher.Invoke(() =>
                    {
                        NewLine(4 * countOfPing, 4 * countOfPing++ + 4, lastPosition, canvasHeight - (Convert.ToInt32(reply.RoundtripTime) * 5), Brushes.GreenYellow, chartCanvas, 2);
                        lblMinValue.Content = $"min {minValue}ms";
                        lblMaxValue.Content = $"max {maxValue}ms";
                        lblAvgValue.Content = $"avg {avgValue}ms";
                        if (4 * countOfPing > chartCanvas.ActualWidth)
                        {
                            chartCanvas.Margin = new Thickness(-newMargin, 23, 73, 10);
                            newMargin         += 4;
                        }
                    });

                    lastPosition = canvasHeight - (Convert.ToInt32(reply.RoundtripTime) * 5);

                    Thread.Sleep(250);
                }
            }
        }
Пример #2
0
        private void StartButton_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(Host))
            {
                tbIpAddress.Text = Host;
            }

            if (tbIpAddress.Text.Equals(""))
            {
                tbIpAddress.Text        = "Host name or IP";
                tbIpAddress.BorderBrush = Brushes.Red;
            }
            else
            {
                Host = tbIpAddress.Text;
                try
                {
                    Pinger.Start(Host);
                    lblAdressIp.Content    = $"Pinging '{Host}'";
                    tbIpAddress.Visibility = Visibility.Hidden;
                    lblAdressIp.Visibility = Visibility.Visible;
                    StopButton.Visibility  = Visibility.Visible;
                    StartButton.Visibility = Visibility.Hidden;
                    lblAvgValue.Visibility = Visibility.Visible;
                    lblMaxValue.Visibility = Visibility.Visible;
                    lblMinValue.Visibility = Visibility.Visible;
                    isStarted = true;

                    DrawGraph();
                }
                catch
                {
                    tbIpAddress.Text        = "Host is incorrect";
                    tbIpAddress.BorderBrush = Brushes.Red;
                }
            }
        }