Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop a process from System.Diagnostics.Process and get the statistics in the end

Tags:

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 + " " + address; 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"); it never goes to the statistics part it hangs at sequence 1 : 64 bytes from 8.8.8.8: icmp_req=1 ttl=46 time=13.9 ms 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...

UPDATE

I have implemented p.StandartInput.Write("/x3"); and it interrupts the ping process but it does not display the statistics and just hangs there.

like image 620
EAK TEAM Avatar asked Mar 20 '17 14:03

EAK TEAM


People also ask

How do I kill system diagnostics Process?

If CloseMainWindow fails, you can use Kill to terminate the process. Kill is the only way to terminate processes that do not have graphical interfaces. You can call Kill and CloseMainWindow only for processes that are running on the local computer.

How do you end a Process in VB?

Furthermore, you could use Process. Kill Method to stop the associated process. The code snippet is as follow.

How do I stop a Process in C#?

Process. Start("cmd.exe","/c taskkill /IM notepad.exe"); This code will close the Notepad (if it is running). Type the program name you want to close with it's extension (.exe).

What is the difference between System Diagnostic and kill?

System. Diagnostics Process. Kill Method System. Diagnostics Forces termination of the underlying process. Immediately stops the associated process. Immediately stops the associated process, and optionally its child/descendent processes. The Kill method forces a termination of the process, while CloseMainWindow only requests a termination.

How do I know if a process has exit?

The Kill method executes asynchronously. After calling the Kill method, call the WaitForExit method to wait for the process to exit, or check the HasExited property to determine if the process has exited. The WaitForExit method and the HasExited property do not reflect the status of descendant processes.

What happens when you close a process in Linux?

The Close method causes the process to stop waiting for exit if it was waiting, closes the process handle, and clears process-specific properties. Close does not close the standard output, input, and error readers and writers in case they are being referenced externally.

What happens when entireprocesstree is set to true?

When entireProcessTree is set to true, processes where the call lacks permissions to view details are silently skipped by the descendant termination process because the termination process is unable to determine whether those processes are descendants. Frees all the resources that are associated with this component.


1 Answers

If you are actually interested in the ping values, why not just use the ping class? https://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping(v=vs.110).aspx

like image 97
Neil Avatar answered Oct 24 '22 21:10

Neil