多线程 循环 更新 网络状态

 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();
            }
   
   
        }

你可能感兴趣的:(网络,多线程,string,object,null,测试)