我有一個循環ping請求,當我點擊按Ctrl +Ç,它打破循環,給我像正常CMD靜,我想的方法,但是當我點擊按Ctrl +ç,我得到禁用控制+ C關閉程序
按任意鍵繼續...... < <
,然後程序關閉。
例如:ping -t google.com < <將ping操作無限循環谷歌,但我需要打破循環,只有當我點擊按Ctrl +ç
private void _t(string website)
{
Ping pingSender = new Ping();
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 10000;
try
{
PingOptions options = new PingOptions(64, false);
PingReply send0 = pingSender.Send(website, timeout, buffer, options);
Console.WriteLine("\nPinging {0} [{1}] With {2} bytes of data :", website, send0.Address.ToString(), send0.Buffer.Length);
while (1 < 2)
{
PingReply reply = pingSender.Send(website, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Reply from {0}: Bytes {1} time={2} TTL={3}", reply.Address.ToString(), reply.Buffer.Length, reply.RoundtripTime/5, reply.Options.Ttl);
}
else
{
Console.WriteLine("Request timed out.");
}
}
}
catch
{
Console.WriteLine("Ping request could not find host {0} ", website + ".Please check the name and try again.");
}
}
但是相反使用while (1<2)
的,我想用這樣的:
while (ConsoleKeyInfo.Equals("Control + c") not clicked) // sure it is wrong , but that`s what I wanna achieve
{
PingReply reply = pingSender.Send(website, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Reply from {0}: Bytes {1} time={2} TTL={3}", reply.Address.ToString(), reply.Buffer.Length, reply.RoundtripTime/5, reply.Options.Ttl);
}
else
{
Console.WriteLine("Request timed out.");
}
}
您的意思是統計數據不是靜態數據嗎? – ono2012 2017-07-25 23:18:47