簡體   English   中英

在一段時間內執行Ping操作

[英]Ping over a period of Time

我有一個Windows窗體應用程序,該應用程序基本上會ping一個IP,然后返回帶有工具提示的圖像,該提示會顯示該IP的rtt。

我要執行的操作是每20秒對ip進行一次ping操作,以便更改表單和圖像。 如果我能得到那個工作,那么我想一些如何存儲也許4 RTT的,然后顯示在工具提示中的平均4。

到目前為止,表單僅能執行一次ping操作,我已經使用了計時器,但是我真的不知道自己在做什么。 到目前為止,這是我的代碼。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.ServiceProcess;
using System.Threading;
using System.ComponentModel;



namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {
            Ping pingClass = new Ping();
            PingReply pingReply = pingClass.Send("10.209.123.123");
            label4.Text = (pingReply.RoundtripTime.ToString());
            //+ "ms");
            label5.Text = (pingReply.Status.ToString());



            if (Convert.ToInt32(label4.Text) > 0 && Convert.ToInt32(label4.Text) < 100)
                this.pictureBox1.Load("greenLAT.png");

            if (Convert.ToInt32(label4.Text) > 100 && Convert.ToInt32(label4.Text) < 200)
                this.pictureBox1.Load("yellowLAT.png");

            if (Convert.ToInt32(label4.Text) > 200 && Convert.ToInt32(label4.Text) < 1000)
                this.pictureBox1.Load("redLAT.png");

            ToolTip tt = new ToolTip();
            tt.SetToolTip(this.pictureBox1, "Your current network delay is " + label4.Text + "ms");

            timer1.Interval = 1000;
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Start();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //MessageBox.Show("Timeout!");
            Refresh();



        }


    }
}

嘗試這個:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Net;
using System.Windows.Forms;
using System.Net.NetworkInformation;



namespace DXWindowsApplication4
{
    public partial class Form2 : Form
    {
        private readonly Timer _timer;
        private readonly Ping _pingClass;
        private readonly IPAddress _ipAddress;
        private readonly int _timeout;

        private Image _greenImage; 
        private Image _yellowImage; 
        private Image _redImage; 

        private int _pingCount;
        private long _avgRtt;

        public Form2()
        {
            InitializeComponent();
            IPAddress.TryParse("98.138.253.109", out _ipAddress); // yahoo.com Ip address
            _timer = new Timer();
            _timeout = 3000;
            _pingClass = new Ping();
            _pingClass.PingCompleted += PingClassPingCompleted;
        }

        void PingClassPingCompleted(object sender, PingCompletedEventArgs e)
        {
            RefreshPing(e.Reply);
        }

        public void FormLoad(object sender, EventArgs e)
        {
            _timer.Tick += TimerTick;
            _timer.Interval = 4000;
            _timer.Start();
        }

        private void TimerTick(object sender, EventArgs e)
        {
            _pingClass.SendAsync(_ipAddress, _timeout);
        }

        private void RefreshPing(PingReply pingReply)
        {
            label4.Text = (pingReply.RoundtripTime.ToString(CultureInfo.InstalledUICulture));
            label5.Text = (pingReply.Status.ToString());

            _avgRtt = (_avgRtt * _pingCount++ + pingReply.RoundtripTime)/_pingCount;

            if (Convert.ToInt32(label4.Text) > 0 && Convert.ToInt32(label4.Text) < 100)
            {
                SetImage(pictureBox1, Images.Green);
            }

            if (Convert.ToInt32(label4.Text) > 100 && Convert.ToInt32(label4.Text) < 200)
            {
                SetImage(pictureBox1, Images.Yellow);
            }

            if (Convert.ToInt32(label4.Text) > 200 && Convert.ToInt32(label4.Text) < 1000)
            {
                SetImage(pictureBox1, Images.Red);
            }

            ToolTip tt = new ToolTip();
            tt.SetToolTip(pictureBox1, "Your average network delay is " + _avgRtt + "ms");
            Refresh();
        }

        private void SetImage(PictureBox pBox, Images images)
        {
            switch (images)
            {
                case Images.Green:
                    if (_greenImage == null)
                    {
                        _greenImage = new Bitmap("greenImage.png");
                    }

                    pictureBox1.Image = _greenImage;
                    break;
                case Images.Yellow:
                    if (_greenImage == null)
                    {
                        _yellowImage = new Bitmap("yellowImage.png");
                    }

                    pictureBox1.Image = _yellowImage;
                    break;
                case Images.Red:
                    if (_redImage == null)
                    {
                        _redImage = new Bitmap("redImage.png");
                    }

                    pictureBox1.Image = _greenImage;
                    break;
                default:
                    throw new InvalidEnumArgumentException("invalid enum name");
            }
        }
    }

    internal enum Images
    {
        Green,
        Yellow,
        Red
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM