0
我有一個窗體應用程序,基本上ping一個IP,然後返回一個圖像的工具提示,顯示rtt到該IP。Ping一段時間
我想要做的就是每隔20秒就有一次表單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();
}
}
}
你在UI線程做平,所以你阻塞UI線程平安的持續時間。 – Servy
哇!這個工作,現在我怎麼可以記錄一個特定的數字可以說10 rtt的,然後顯示平均內的工具提示>?非常感謝你的幫助! – user1836162
@Servy它只是想法,而不是真正的代碼。它離很好的代碼很遠。 –