C#如何10秒自动ping服务器一次,显示服务器的链接状态 C#如何10秒自动ping服务器一次,显示服务器的链接状态 在线等,谢谢好心的朋友帮忙发下代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 拖放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(); 看我的帖子希望对你有帮助http://blog.csdn.net/tigerleq/archive/2008/02/28/2127602.aspx 代码有点长用里面的pingH 不建议使用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网关 能给出个实例吗,邮箱[email protected] Thanks! Tcplistener问题...请教~ 程序初始化失败问题 c#引用C++DLL时char*返回值如何获取? C# 管理软件如何实现网络版? 如何从数据库中得到单一的值并将值写入文本框 开发window form在没有像在web form的imagemap的控件阿---急用! Javascript问题,老调试不好,请求高手解答,谢谢! 55 高手帮忙。两个界面之间传值 求一个较复杂的list<T>去重复 正则表达式!!!!! 大哥,为什么不能绑定数据到下拉框,谢谢 publickeytoken可以代替公钥?
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();
希望对你有帮助http://blog.csdn.net/tigerleq/archive/2008/02/28/2127602.aspx
用里面的pingH
看看代码吧
在此: 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网关