如何从system.diagnostics.process停止进程并继续读取直到结束 [英] How to stop a process from system.diagnostics.process and continue reading till the end

查看:131
本文介绍了如何从system.diagnostics.process停止进程并继续读取直到结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码,但是当我停止该过程时,它无法获得ping统计信息:



I'm using this code but when i stop the process it not get the ping statistics :

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "ping";
p.StartInfo.Arguments = "-c " + count + " -i " + interval + " -s " + buffer + " -W " + timeout + " " + adresa;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;


string readData = "";

DateTime dt = DateTime.Now.AddSeconds(5);
if (p.Start())
{
    Scanner scanner = new Scanner(p.StandardOutput.BaseStream);

    while (scanner.HasNextLine)
    {
        readData =  scanner.NextLine().ToString();
        Console.WriteLine(readData.ToString());

        if (!string.IsNullOrEmpty(readData) && !readData.StartsWith("---"))
        {
            Match M = Regex.Match(readData, @"^[\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
               if(dt > DateTime.Now)
               {
                   p.StandartInput.Write("\x3");
                }
            }
            else
            {
                Match M1 = Regex.Match(readData, @"^rtt [^0-9]*([\d][^\/]+)\/([^\/]+)\/([^\/]+)\/([^ ]+) ms$");

                if (M1 != null && M1.Success)
                {
                    float avgPingTime = 0;
                    float maxPingTime = 0;
                    float minPingTime = 0;

                    string minPingString = M1.Groups[1].Value;
                    string avgPingString = M1.Groups[2].Value;
                    string maxPingString = M1.Groups[3].Value;

                    // Now parse that value
                    float.TryParse(minPingString, System.Globalization.NumberStyles.Float, System.Globalization.NumberFormatInfo.InvariantInfo, out minPingTime);
                    float.TryParse(avgPingString, System.Globalization.NumberStyles.Float, System.Globalization.NumberFormatInfo.InvariantInfo, out avgPingTime);
                    float.TryParse(maxPingString, System.Globalization.NumberStyles.Float, System.Globalization.NumberFormatInfo.InvariantInfo, out maxPingTime);

                    Console.WriteLine(String.Format("Min Time : {0} , AVG {2} ms, Max Time {1}", minPingTime, maxPingTime, avgPingTime));
                }
            }
        }
    }
}







不使用






Without using

if(dt > DateTime.Now)
{
     p.StandartInput.Write("\x3");
}



结果显示如下:




the result display like this :

64 bytes from 8.8.8.8: icmp_req=1 ttl=46 time=13.9 ms
64 bytes from 8.8.8.8: icmp_req=2 ttl=46 time=13.9 ms
64 bytes from 8.8.8.8: icmp_req=3 ttl=46 time=13.9 ms


--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 3016ms
rtt min/avg/max/mdev = 13.910/13.926/13.951/0.010 ms>







但如果我使用




but if I stop the ping using

p.StandartInput.Write("\x3");

停止ping,它永远不会进入统计部分它挂起在序列1并且不继续读取,如何在停止ping过程后显示统计信息?



换句话说我的问题是当用户想要停止ping它应该显示用于ping的特定时间的统计数据...



我尝试过:



尝试:

it never goes to the statistics part it hangs at sequence 1 and dont continue reading , how to show the statistics after stoping the ping process ?

in other words my problem is when the user want to stop the ping it should display the statistics for the certain time that it was used to ping...

What I have tried:

Trying with :

p.StandartInput.WriteLine("\x3");



和:


and with :

scanner.Reset;





但问题仍然存在......



But still the same problem ...

推荐答案

);

if (M1!= null && M1.Success)
{
float avgPingTime = 0 ;
float maxPingTime = 0 ;
float minPingTime = 0 ;

string minPingString = M1.Groups [ 1 ]。
string avgPingString = M1.Groups [ 2 ]。
string maxPingString = M1.Groups [ 3 ]。

// 现在解析该值
float .TryParse(minPingString,System.Globalization.NumberStyles.Float,System.Globalization.NumberFormatInfo.InvariantInfo, out minPingTime);
float .TryParse(avgPingString,System.Globalization.NumberStyles.Float,System.Globalization.NumberFormatInfo.InvariantInfo, out avgPingTime);
float .TryParse(maxPingString,System.Globalization.NumberStyles.Float,System.Globalization.NumberFormatInfo.InvariantInfo, out maxPingTime);

Console.WriteLine( String .Format( Min Time:{0},AVG {2} ms,Max Time {1},minPingTime,maxPingTime,avgPingTime));
}
}
}
}
}
"); if (M1 != null && M1.Success) { float avgPingTime = 0; float maxPingTime = 0; float minPingTime = 0; string minPingString = M1.Groups[1].Value; string avgPingString = M1.Groups[2].Value; string maxPingString = M1.Groups[3].Value; // Now parse that value float.TryParse(minPingString, System.Globalization.NumberStyles.Float, System.Globalization.NumberFormatInfo.InvariantInfo, out minPingTime); float.TryParse(avgPingString, System.Globalization.NumberStyles.Float, System.Globalization.NumberFormatInfo.InvariantInfo, out avgPingTime); float.TryParse(maxPingString, System.Globalization.NumberStyles.Float, System.Globalization.NumberFormatInfo.InvariantInfo, out maxPingTime); Console.WriteLine(String.Format("Min Time : {0} , AVG {2} ms, Max Time {1}", minPingTime, maxPingTime, avgPingTime)); } } } } }







不使用






Without using

if(dt > DateTime.Now)
{
     p.StandartInput.Write("\x3");
}



结果显示如下:




the result display like this :

64 bytes from 8.8.8.8: icmp_req=1 ttl=46 time=13.9 ms
64 bytes from 8.8.8.8: icmp_req=2 ttl=46 time=13.9 ms
64 bytes from 8.8.8.8: icmp_req=3 ttl=46 time=13.9 ms


--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 3016ms
rtt min/avg/max/mdev = 13.910/13.926/13.951/0.010 ms>







但如果我使用




but if I stop the ping using

p.StandartInput.Write("\x3");

停止ping,它永远不会进入统计部分它挂起在序列1并且不继续读取,如何在停止ping过程后显示统计信息?



换句话说我的问题是当用户想要停止ping它应该显示用于ping的特定时间的统计数据...



我尝试过:



尝试:

it never goes to the statistics part it hangs at sequence 1 and dont continue reading , how to show the statistics after stoping the ping process ?

in other words my problem is when the user want to stop the ping it should display the statistics for the certain time that it was used to ping...

What I have tried:

Trying with :

p.StandartInput.WriteLine("\x3");



和:


and with :

scanner.Reset;





但问题仍然存在......



But still the same problem ...


这篇关于如何从system.diagnostics.process停止进程并继续读取直到结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆