Пример #1
0
        private void PingThread(object ServerIP)
        {
            Ping_cmdTextBox.Invoke(new MethodInvoker(delegate
            {
                Ping_cmdTextBox.AppendText("正在 Ping " + ServerIP.ToString() + ":\r\n");
            }));
            Ping p         = new Ping();
            int  pcount    = 0;
            long MinRT     = -1;
            long MaxRT     = -1;
            long AvgRT     = -1;
            long TotalRT   = 0;
            int  SuccessRT = 0;

            while (pcount < PingCount || PingCount == 0)
            {
                PingReply pr = p.Send(ServerIP.ToString());
                Ping_cmdTextBox.Invoke(new MethodInvoker(delegate
                {
                    if (pr.Status == IPStatus.Success)
                    {
                        SuccessRT++;
                        Ping_cmdTextBox.AppendText("来自 " + pr.Address.ToString() + " 的答复:字节=" + pr.Buffer.Length + " 时间=" + pr.RoundtripTime + " TTL=" + pr.Options.Ttl + "\r\n");
                        if (MinRT == -1 || pr.RoundtripTime < MinRT)
                        {
                            MinRT = pr.RoundtripTime;
                        }
                        if (MaxRT == -1 || pr.RoundtripTime > MaxRT)
                        {
                            MaxRT = pr.RoundtripTime;
                        }
                        TotalRT += pr.RoundtripTime;
                        AvgRT    = TotalRT / SuccessRT;
                    }
                    else
                    {
                        Ping_cmdTextBox.AppendText(pr.Status.ToString() + "\r\n");
                    }
                }));
                pcount++;
                if (IsPingAllow == false)
                {
                    break;
                }
                Thread.Sleep(Interval);
            }
            this.Invoke(new MethodInvoker(delegate
            {
                Ping_cmdTextBox.AppendText("Ping统计信息:\r\n");
                Ping_cmdTextBox.AppendText("数据包:已发送 = " + pcount + ",已接收 = " + SuccessRT + ",丢失 = " + (pcount - SuccessRT) + " <" + (int)((1 - (float)SuccessRT / (float)pcount) * 100) + "%丢失>\r\n");
                Ping_cmdTextBox.AppendText("往返行程估计时间:最短 = " + MinRT + "ms,最长 = " + MaxRT + "ms,平均 = " + AvgRT + "ms\r\n\r\n");
                Ping_button.Enabled = true;
                Ping_button.Text    = "Ping";
            }));
            IsPingAllow = false;
        }
Пример #2
0
 private void MSI_Clear_Click(object sender, EventArgs e)
 {
     Ping_cmdTextBox.Clear();
 }