3

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?

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
D.R.
  • 20,268
  • 21
  • 102
  • 205
  • 1
    Try to [read input](https://stackoverflow.com/a/139604/1997232)? – Sinatr May 11 '20 at 11:05
  • 1
    Did you close the `StandardInput` stream? – Pavel Anikhouski May 11 '20 at 11:20
  • You should have the opposite problem, since `StandardInput.WriteLine()` *confirms* more than one *pause* at the time. Unless you set `AutoFlush= false`. Is there just a `pause` in that batch file or something else? Is it a batch (.bat, .cmd) or a different type of *script*? With these settings, you should see the process output in VS Output window... – Jimi May 11 '20 at 11:27
  • @PavelAnikhouski: Yep, I forgot to close StandardInput! Thanks, resolved. You can formulate your comment as an answer, then I can mark it as accepted. – D.R. May 11 '20 at 12:28

1 Answers1

3

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();
Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66