I'm trying to start a Process designed to be used in the command line and get the output of that file as it's running. The Process has a completion percentage, which is what I want my program to get.
I tried this code:
Console.Write("Input file name: ");
string fileName = Console.ReadLine();
Process process = new();
process.StartInfo.FileName = @"example.exe";
process.StartInfo.Arguments = $"--file {fileName}";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.OutputDataReceived += (sender, eventArgs) =>
{
Console.WriteLine(eventArgs.Data);
};
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
However, it only prints out the output after the process has exited (and therefore finished it's job), which defeats the purpose of getting the percentage completed when it's already done. Removing the process.WaitForExit()
makes the program close immediately after it starts.