1

I have an exe file that I call it from my c# code by using Process namespace in c#.
I catch output by using ReadToEnd() method. (in exe source code i used a print method for my desired output).
the problem is when my output is getting larger the executing of exe file is got crashed and hanged.
Is there any limitation of output size when calling exe function?

Process p = new Process();
p.StartInfo.FileName = command;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.Arguments = args;
p.Start();
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();
return output;
Jafar Gh
  • 137
  • 2
  • 12
  • First you need to actually read the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standardoutput?view=netframework-4.8) for this particular task. Another thing is I believe you should try to go for an asynchronous approach which can also be found [here](https://stackoverflow.com/questions/12566166/how-to-asynchronously-read-the-standard-output-stream-and-standard-error-stream) – DevX Jun 25 '19 at 06:50
  • What is the error/exception you get? – Markus Deibel Jun 25 '19 at 06:51
  • I don't get any error message. but console page while running exe file stays forever. – Jafar Gh Jun 25 '19 at 06:54

2 Answers2

2

What you're experiencing is a deadlock. That situation is described in the documentation. In order to avoid it, make sure you read the output before waiting for the child process to exit.

p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
max
  • 466
  • 3
  • 7
1

I remember having the same problem as you're describing here, and what I did back then was something like this:

private string RunProcess(string command, string args)
{
    StringBuilder sb = new StringBuilder();
    Process p = new Process();
    p.StartInfo.FileName = command;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.Arguments = args;
    p.OutputDataReceived += (sender, eventArgs) => { sb.Append(eventArgs.Data); };
    p.Start();
    p.WaitForExit();
    return sb.ToString();
}
hager
  • 313
  • 1
  • 7