C#如何10秒自动ping服务器一次,显示服务器的链接状态 在线等,谢谢好心的朋友帮忙发下代码

解决方案 »

  1.   

    拖放timer控件,在Tictk事件里写:this.textBox2.Text = "";
                ProcessStartInfo start = new ProcessStartInfo("Ping.exe");
                start.Arguments =this.textBox1.Text;
                start.CreateNoWindow = true;
                start.RedirectStandardOutput = true;
                start.RedirectStandardInput = true;
                start.UseShellExecute = false;
                Process p = Process.Start(start);
                StreamReader reader = p.StandardOutput;
                string line = reader.ReadLine();
                while (!reader.EndOfStream)
                {
                    textBox2.AppendText(line + "\n");
                    line = reader.ReadLine();
                }
                p.WaitForExit();
                p.Close();
                reader.Close();
      

  2.   

    看我的帖子
    希望对你有帮助http://blog.csdn.net/tigerleq/archive/2008/02/28/2127602.aspx
      

  3.   

    代码有点长
    用里面的pingH
      

  4.   

    不建议使用2楼的方法 第一 杀毒软件会叫 第二 占资源 第三 慢
    看看代码吧
    在此:        private void timerping_Tick(object sender, EventArgs e)
            {
                string who = gateway .ToString ();            AutoResetEvent waiter = new AutoResetEvent(false);            Ping pingSender = new Ping();
                pingSender.PingCompleted += new PingCompletedEventHandler(PingCompletedCallback);
                string data = "NCV_netcheck";
                byte[] buffer = Encoding.ASCII.GetBytes(data);
                int timeout = timerping .Interval ;
                PingOptions options = new PingOptions(64, true);
                pingSender.SendAsync(who, timeout, buffer, options, waiter);
            }
            public void PingCompletedCallback(object sender, PingCompletedEventArgs e)
            {
                string gatestr="";
                long delaygate = 100;
                if (e.Cancelled)
                {
                   gatestr  = "Ping canceled.";
                    ((AutoResetEvent)e.UserState).Set();
                }
                if (e.Error != null)
                {
                    gatestr = "N/A";
                    gatestr = e.Error.ToString();
                    ((AutoResetEvent)e.UserState).Set();
                }            PingReply reply = e.Reply;            
                if (reply.Status == IPStatus.Success)
                {
                    delaygate = reply.RoundtripTime+1;
                    gatestr = delaygate.ToString();
                    if (delaygate > 99) delaygate = 100;
                    this.barGW.Value = (int)delaygate+2;
                }
                ((AutoResetEvent)e.UserState).Set();
                this.gate_way.Text = gatestr+"ms";   
            }gatestr就是延迟了
    希望对你有帮助
    做个timer重复  重复事件不要小于超时事件 不然你会发现你机子在飞奔(100%)运行ping网关
      

  5.   

    能给出个实例吗,邮箱[email protected] Thanks!