D.R.
D.R.

Reputation: 21194

Process.WaitForExit hangs - but I only redirect standard input?

I'm trying to execute a batch script which contains a pause statement at the end which I want to confirm:

var psi = new ProcessStartInfo(path, arguments);
psi.RedirectStandardInput = true;
psi.UseShellExecute = false;
psi.WorkingDirectory = workDir;

var p = Process.Start(psi);
p.StandardInput.WriteLine();
p.WaitForExit();

However, this code hangs forever in WaitForExit although the process has already finished. I know that you have to read the buffer when redirecting standard output / error. Is there something special about redirecting standard input as well?

Upvotes: 3

Views: 564

Answers (1)

Pavel Anikhouski
Pavel Anikhouski

Reputation: 23228

You may refer to RedirectStandardInput documentation and example. You should close the StandardInput stream after writing a line to it to properly handle a pause statement

p.StandardInput.WriteLine();
p.StandardInput.Close();
p.WaitForExit();

Upvotes: 3

Related Questions