execute batch file does not execute all command lines

J

John Grandy

I am using the following code to execute a batch file which contains
multiple commands, each on a separate line. Each command-line creates a
file. What I am finding is that only some of the files are being created ( 7
out of 11 ).

If I then run the batch file directly ( either by double-clicking, or
pasting its path into cmd.exe window ), all files are created ( 11 out of
11 ).


private void executeBatchFile(string pathBatchFile,
string pathWorkingDirectory,
Label outputDump)
{

ProcessStartInfo processStartInfo = new
ProcessStartInfo(pathBatchFile);
processStartInfo.RedirectStandardOutput = true;
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.UseShellExecute = false;
processStartInfo.WorkingDirectory = pathWorkingDirectory;

Process process = Process.Start(processStartInfo);
process.WaitForExit(30000);
if (process.HasExited)
{
string output = process.StandardOutput.ReadToEnd();
outputDump.Text = output;
}

}
 
P

Peter Duniho

John said:
I am using the following code to execute a batch file which contains
multiple commands, each on a separate line. Each command-line creates a
file. What I am finding is that only some of the files are being created ( 7
out of 11 ).

If I then run the batch file directly ( either by double-clicking, or
pasting its path into cmd.exe window ), all files are created ( 11 out of
11 ).

Impossible to know exactly what's wrong without a concise-but-complete
code example. However, you should be aware that if you redirect the
standard output or standard error streams, you _must_ read from them
regularly enough to ensure that the process does not get blocked.

One possibility, given the code you posted, is that the standard output
buffer gets filled, preventing the process from continuing any further.

The other question is what you actually do with the process if the
WaitForExit() times out. If you _really_ want to ensure that the batch
file runs to completion, in addition to making sure that the output and
error streams don't get blocked, you need to not have a timeout when
waiting for the process to exit.

If neither of the above seems helpful, post a concise-but-complete code
example that reliably demonstrates the problem.

Pete
 
J

John Grandy

I set

processStartInfo.RedirectStandardOutput = false;

and now all command-lines are executed ( 11 files are created ).

What would be a solution to periodically capture & flush a non-standard
output buffer ?
 
P

Peter Duniho

John said:
I set

processStartInfo.RedirectStandardOutput = false;

and now all command-lines are executed ( 11 files are created ).

What would be a solution to periodically capture & flush a non-standard
output buffer ?

The easiest way is, instead of calling WaitForExit(), to just have a
loop that reads one line at a time from the StandardOutput reader until
you reach the end of the output.

Since you apparently want the output copied to your Label control
anyway, you can either append the text to the Label as each line is
written, or save the output to a StringBuilder and assign it to the
Label when you're done.

For example:

private void executeBatchFile(
string pathBatchFile,
string pathWorkingDirectory,
Label outputDump)
{

ProcessStartInfo processStartInfo =
new ProcessStartInfo(pathBatchFile);

processStartInfo.RedirectStandardOutput = true;
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.UseShellExecute = false;
processStartInfo.WorkingDirectory = pathWorkingDirectory;

Process process = Process.Start(processStartInfo);

StringBuilder sb = new StringBuilder();
string strLine;

while ((strLine = process.StandardOutput.ReadLine()) != null)
{
sb.AppendLine(strLine);
}

outputDump.Text = sb.ToString();
}

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top