3

The problem is that the WaitForExit does not wait until the batch file quits. It comes back almost right away.

I'm starting my batch file as follows:

            ProcessStartInfo startInfo = new ProcessStartInfo(batchFile);
            startInfo.UseShellExecute = true;
            startInfo.Arguments = arguments;

            using (Process p = Process.Start(startInfo))
            {
                p.WaitForExit();
            }

I tried with and without UseShellExecute.

101010
  • 14,866
  • 30
  • 95
  • 172
  • 2
    [This answer](http://stackoverflow.com/a/7101534/16623) may help. – Michael Todd Oct 08 '13 at 19:04
  • if WaitForExit() dosen't work try to follow [this](http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exited.aspx) – Tinwor Oct 08 '13 at 19:11
  • That didn't work either. It must be something unique about this batch file. The batch file calls other processes, but does wait until they all quit before it returns. – 101010 Oct 08 '13 at 19:38

2 Answers2

1

You could try running cmd with a "/c yourbatchfile" as command line arguments instead.

mageos
  • 1,216
  • 7
  • 15
0

It seems that you can do it redirecting the StdOut and read it until it closes.

Took this idea from this similar question.

Adapting your snippet, that would be:

ProcessStartInfo startInfo = new ProcessStartInfo(batchFile);
//startInfo.UseShellExecute = true;
startInfo.Arguments = arguments;
startInfo.RedirectStandardOutput = true;

Process p = Process.Start(startInfo);
String output = proc.StandardOutput.ReadToEnd();
Community
  • 1
  • 1
J.Hudler
  • 1,238
  • 9
  • 26