When ProcessStartInfo.RedirectStandardOutput is true, the process's output is redirected to Process.StandardOutput.
But in the OP code the Process.StandardOutput
isn't read and the buffer fills up to its limit. When the limit is reached, the process can't write Process.StandardOutput
and wait some free space.
Same story with ProcessStartInfo.RedirectStandardError and Process.StandardError
One solution is to set ProcessStartInfo.RedirectStandardOutput
and ProcessStartInfo.RedirectStandardError
to false.
Other solution if you want the output, you can redirect the output to other stream. See this example where the output is redirected to a file :
void Redirect(StreamReader output, StreamWriter to)
{
string textLine;
while ((textLine = output.ReadLine()) != null)
{
to.WriteLine(textLine);
}
}
bool createShell = true;
//generate output pdf
using var pdfGeneratorProcess = new System.Diagnostics.Process {
StartInfo = new ProcessStartInfo {
UseShellExecute = createShell,
CreateNoWindow = !createShell, // I inversed the condition
RedirectStandardError = !createShell,
RedirectStandardOutput = !createShell,
WorkingDirectory = dir,
FileName = "java",
Arguments = $"-cp "fop-2.0.jar;lib/*"; FOPLocalPDFGenerator {rendererFile} {inputFile} {outputFile}"
}
};
_logger.LogInformation("{CMD}", $"{pdfGeneratorProcess.StartInfo.FileName} {pdfGeneratorProcess.StartInfo.Arguments}");
pdfGeneratorProcess.Start();
using (StreamWriter writer = File.CreateText(@"C:Templog.txt"))
{
var standardOutputThread = new Thread(new ThreadStart(() => Redirect(pdfGeneratorProcess.StandardOutput, writer)));
var errorOutputThread = new Thread(new ThreadStart(() => Redirect(pdfGeneratorProcess.StandardError, writer)));
standardOutputThread.Start();
errorOutputThread.Start();
standardOutputThread.Join();
errorOutputThread.Join();
}
pdfGeneratorProcess.WaitForExit();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…