所以,我有这个工作代码来显示来自var k = document.getElementsByTagName("td");
for( var i = 0; i < k.length; i++ ){
k[i].addEventListener("click", function(evt) {
k[i].textContent = "Success!";
});
}
System.Diagnostics.Process
我以这种方式处理字符串:
Process P = new Process();
P.StartInfo.FileName = "ping";
P.StartInfo.Arguments = "-c 3 8.8.8.8"; // Take 3 samples to 8.8.8.8
P.StartInfo.UseShellExecute = false;
P.StartInfo.RedirectStandardOutput = true;
string readData = "";
if (P.Start())
readData = P.StandardOutput.ReadToEnd(); // This will also wait for the process to at least close its stdout
Console.Write(readData.ToString()); // Doing this, you will see how the
List<string> Lines = new List<string>(readData.Replace("\r\n", "\n").Split('\n'));
while (Lines.Count > 0 && !Lines[0].StartsWith("---"))
{
Match M = Regex.Match(Lines[0], @"^[\d]+ bytes from ([^:]+): [^ ]+ ttl=([\d]+) time=([^ ]+) ms");
if (M != null && M.Success)
{
string IP = M.Groups[1].Value;
string TTL = M.Groups[2].Value;
string timeStr = M.Groups[3].Value;
Console.WriteLine(String.Format("Ping to {0} took {2} ms with a ttl of {1}", IP, TTL, timeStr));
// Parsing the timeStr will work the same way as above
}
Lines.RemoveAt(0);
}
等待标准输出关闭,然后处理字符串。
我的问题是如何实时处理每行的字符串行,以及如何在特定时间内停止该过程并最终得到统计数据。
答案 0 :(得分:2)
System.IO.StreamReader
有一个名为ReadLine
的方法,您可以使用它:
if (P.Start()){
DateTime endTime = DateTime.Now.AddSeconds(5);
while(!P.HasExited){
readData = P.StandardOutput.ReadLine(); // This will wait for the next line to be output completely
Match M = Regex.Match(Lines[0], @"^[\d]+ bytes from ([^:]+): [^ ]+ ttl=([\d]+) time=([^ ]+) ms");
if (M != null && M.Success)
{
string IP = M.Groups[1].Value;
string TTL = M.Groups[2].Value;
string timeStr = M.Groups[3].Value;
Console.WriteLine(String.Format("Ping to {0} took {2} ms with a ttl of {1}", IP, TTL, timeStr));
}
if (endTime > DateTime.Now)
P.Kill();
}
}
我引入了一个名为endTime
的变量,该变量在未来5秒内被初始化,当达到该时间时,该进程被终止,导致循环终止,因为P.HasExited
现在变为true
。