Search code examples
c#processprocessstartinfo

Program doesn’t terminate when using processes


Using the ProcessStartInfo and Process I would like start a program (e g getdiff.exe) and then read all the output that program produces. Later I will use the data in a more constructive way put right now I just want to print the data to ensure it works. However the program doesn’t terminate as it should. Does anyone se why? Thank you in advanced.

ProcessStartInfo psi = new ProcessStartInfo("getdiff.exe");
psi.Arguments = "DIFF";
psi.UseShellExecute = false;                
psi.RedirectStandardInput = true;
psi.WorkingDirectory = "c:\\test";

Process p = Process.Start(psi);
string read = p.StandardOutput.ReadToEnd();
p.WaitForExit();

Console.WriteLine(p);
Console.WriteLine("Complete");

p.Close();

Changing the program to this got it working correctly:

ProcessStartInfo psi = new ProcessStartInfo("getdiff.exe");
psi.Arguments = "DIFF";
psi.UseShellExecute = false;                
psi.RedirectStandardInput = true;
psi.WorkingDirectory = "c:\\test";

Process p = Process.Start(psi);
StreamReader read = p.StandardOutput;

while (read.Peek() >= 0)
    Console.WriteLine(read.ReadLine());

Console.WriteLine("Complete");
p.WaitForExit();
p.Close();

Solution

  • ProcessStartInfo psi = new ProcessStartInfo("getdiff.exe");
    psi.Arguments = "DIFF";
    psi.UseShellExecute = false;                
    psi.RedirectStandardInput = true;
    psi.WorkingDirectory = "c:\\test";
    
    Process p = Process.Start(psi);
    StreamReader read = p.StandardOutput;
    
    while (read.Peek() >= 0)
        Console.WriteLine(read.ReadLine());
    
    Console.WriteLine("Complete");
    p.WaitForExit();
    p.Close();