Problem with executing large batch file from C #

I am executing a batch file from my C # application using the System.Diagnostics classes, updating my GUI with the output on this path, but as soon as my batch file exceeds a certain number of lines, the process just hangs. The exact number of lines seems to be changing, but I was able to play it with a simple batch file that prints "Hello Kitty" 316 times:

@echo off echo Hello Kitty echo Hello Kitty 

and etc.

If I delete the 316th line, the batch file does a fine, and the forms application behaves as expected, but any other lines cause the process to pause indefinitely, without even producing one of the first 300 hi-kittens.

Here is my code to execute the batch file:

 process = new System.Diagnostics.Process(); process.StartInfo.FileName = batchName; process.StartInfo.Arguments = " < nul"; process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.UseShellExecute = false; process.Start(); 

with those indicated elsewhere:

 protected Process process; protected StreamReader output; 

My main form does something like this (simplified a bit):

 string output; while (!proc.Process.HasExited) { proc.Process.WaitForExit(200); if (proc.Process.HasExited) { output = proc.Output.ReadToEnd(); rtbStatus.AppendText(output); } Application.DoEvents(); } 

I don’t understand why he does this, and no examples that I find on the net mention the size limit for batch files. Please inform.

+4
source share
1 answer

You need to constantly read the conclusion - you cannot wait for the end.

The output is buffered, so a small amount (usually around 4 KB) can be written before the process freezes.

An alternative is to direct the output to a file and then read the file.

+7
source

All Articles