How can I check if the process has completed?

is there a way to indicate if the process, like a standard calculator, was a day off or not,
I need this because I have this line:

sr = p1.StandardOutput; 

and i need to do this:

 s = sr.ReadLine(); 

only if there is a way out of p1 in the calculator, for example, there is no way out, so the program gets stuck after ReadLine .
thank you all.

code:

 while (i < asProcesses.Length - 1) { if ((i + 1) == asProcesses.Length - 1 && sOutRedirect != "") break; p1.StartInfo.RedirectStandardOutput = true; p1.StartInfo.FileName = asProcesses[i]; p1.StartInfo.UseShellExecute = false; if(i==0) p1.Start(); sr = p1.StandardOutput; Process p2 = new Process(); p2.StartInfo.RedirectStandardInput = true; p2.StartInfo.FileName = asProcesses[i + 1]; p2.StartInfo.UseShellExecute = false; p2.Start(); sw = p2.StandardInput; while (!sr.EndOfStream && s != null) { s = sr.ReadLine(); if (s != null) { sw.WriteLine(s); } } if (sw != null) sw.Close(); if (sr != null) sr.Close(); i++; } 
+1
source share
2 answers
 void foo() { System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = "c:\\windows\\system32\\ping.exe"; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.UseShellExecute = false; p.EnableRaisingEvents = true; p.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(p_OutputDataReceived); p.Start(); p.BeginOutputReadLine(); } void p_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e) { string s = e.Data; // process s } 
0
source

If all processes exit after execution, use this instead of the internal one, but:

 p1.WaitForExit(); sw.Write(sr.ReadToEnd()); 

If you want processes not to run:

 int i = 0; while (!p1.HasExited && i < maxWaits) { Thread.Sleep(delay); i++; } sw.Write(sr.ReadToEnd()); //Kill process if running: if (!p1.HasExited) { try { p1.Kill(); } catch { } } 

Edit:

It seems you are trying to bind the output of each process to the next. If in this case you are missing p1 = p2 at the end of the loop.
Also, consider moving the first run from the loop: it will make your code more readable. Setting p1 StartInfo should be moved to the if (i == 0) block if (i == 0) if you leave it this way. Moving the output read by the last process would not be a bad idea, in my opinion ...

Edit:

This is my solution (with timeout):

  int maxWaits = 10; // Wait 1 second at most. int delay = 100; var p = new Process(); p.StartInfo.RedirectStandardOutput = true; p.StartInfo.FileName = asProcesses[0]; p.StartInfo.UseShellExecute = false; p.Start(); foreach (var path in asProcesses.Skip(1)) { var p2 = new Process(); p2.StartInfo.FileName = path; p2.StartInfo.RedirectStandardInput = true; p2.StartInfo.RedirectStandardOutput = true; p2.StartInfo.UseShellExecute = false; { int i = 0; while (!p.HasExited && i < maxWaits) { p2.StandardInput.Write(p.StandardOutput.ReadToEnd()); //Redirect IO. This line means that the second process can start calculations if the first is long-running and writes its output progressively. Thread.Sleep(delay); i++; } } p2.StandardInput.Write(p.StandardOutput.ReadToEnd()); //Redirect last output from p. { //Kill process if still running: if (!p.HasExited) { try { p.Kill(); } catch { } } } } { int i = 0; while (!p.HasExited && i < maxWaits) { Thread.Sleep(delay); i++; } } string result = p.StandardOutput.ReadToEnd(); { if (!p.HasExited) { try { p.Kill(); } catch { } } } 

Edit:

An algorithm waiting for each process to exit:

  var p = new Process(); p.StartInfo.RedirectStandardOutput = true; p.StartInfo.FileName = asProcesses[0]; p.StartInfo.UseShellExecute = false; p.Start(); foreach (var path in asProcesses.Skip(1)) { var p2 = new Process(); p2.StartInfo.FileName = path; p2.StartInfo.RedirectStandardInput = true; p2.StartInfo.RedirectStandardOutput = true; p2.StartInfo.UseShellExecute = false; p.WaitForExit(); p2.StandardInput.Write(p.StandardOutput.ReadToEnd()); } p.WaitForExit(); string result = p.StandardOutput.ReadToEnd(); 

I moved the first process out of the loop to get rid of the conditional. Thus, the control flow is simpler and easier to add code binding to the first process.

0
source

Source: https://habr.com/ru/post/1342017/


All Articles