多线程 循环 更新 网络状态

 using System.Threading;

private ToolStrip ts = null;
  private ToolStripLabel tlbl=new ToolStripLabel();
  private string PingStr=string.Empty;
  private string ServerIP=AsapPublic.Globe.RemotServerAdd;

//(全局变量)
  private SynchronizationContext CurSynContext = null;
  private void myEventForCallback(object state)
        {
           
   string strRst;
         do
    {
          
          tlbl.Text=CmdPing(ServerIP,false,out strRst);
    PingStr=strRst;
          System.Threading.Thread.Sleep(60000);
     
         } while(1==1);

         
        }

        private void ThreadStar()
        {
           
         CurSynContext.Post(new SendOrPostCallback(myEventForCallback), null);
        }
        private static string CmdPing(string strIp,bool show,out string OutStr)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;

            p.Start();
            p.StandardInput.WriteLine("ping -n 5 "+strIp);
            p.StandardInput.WriteLine("exit");
            string strRst = p.StandardOutput.ReadToEnd();
            OutStr=strRst;
            p.Close();
            if(show)
             return strRst;
   try
   {
    strRst=strRst.Replace("平均","Average");
    string[] arry=Regex.Split(strRst,"Average = ",RegexOptions.IgnoreCase);
    int Ms=Convert.ToInt32(Regex.Split(arry[1],"ms",RegexOptions.IgnoreCase)[0]);
    if(Ms<50)
     return strRst="网络质量:优良,能正常使用 "+Ms.ToString()+"ms ";
    if(Ms<200)
     return strRst="网络质量:中等,保存缓慢 "+Ms.ToString()+"ms ";
    if(Ms<500)
     return strRst="网络质量:差,查询非常缓慢,请检查局域网内是否有人在下载、看网络视频、QQ传东西等占用网络带宽的情况 "+Ms.ToString()+"ms ";
    if(Ms>500)
     return strRst="网络质量:极差,不能正常使用软件 "+Ms.ToString()+"ms ";
   }
   catch
   {
    strRst="网络已断开,请检查您是否能上网";
   }
            return strRst;
        }
       
  private void EVENT_tlbl_OnClick (object sender, EventArgs e)
  {
   
   
   MessageBox.Show(PingStr);
  }

 

 

 public new void OutLookMain_Load(object sender, EventArgs e)
        {
   ServerIP=AsapPublic.Globe.RemotServerAdd;
         this.Text=AsapPublic.Globe.AccountDisplayName;   
            foreach (Control c in this.MdiParent.Controls)
            {
                if (c.GetType() == typeof(ToolStrip))
                {
                    ts = c as ToolStrip;
                    break;
                }
            }

            if (ts.Items.Count<6)
            {
             
             tlbl.Text = "网络测试中...";
       tlbl.Alignment= ToolStripItemAlignment.Right;
       tlbl.Click+=this.EVENT_tlbl_OnClick;
      
       ts.Items.Add(tlbl);
       // ReadIni();
             CurSynContext = SynchronizationContext.Current;
             ThreadStar();
            }
   
   
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值