0

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.

TheTank20
  • 89
  • 1
  • 6
  • Side note: many programs don't print progress to standard output. I'd strongly recommend creating [mre] for your own testing for both sides so you know for sure that output is generated by the second program continuously. (Ideally, you'd provide that code in the question too... as there is no way to know if indeed the *other* process actually outputs anything). – Alexei Levenkov Apr 13 '23 at 00:36
  • The following may be helpful: https://stackoverflow.com/a/71344930/10024425 (you can ignore the first part about setting it up to run as administrator). Whether or not it works will depend on how the Console app outputs the progress. – Tu deschizi eu inchid Apr 13 '23 at 00:53
  • Some programs change behaviour when stdout is a TTY vs a redirected stream. Windows 10 has introduced a new OS API for creating a console CONPTY. Maybe someone could adapt this sample into a nuget package? https://github.com/microsoft/terminal/tree/main/samples/ConPTY/MiniTerm – Jeremy Lakeman Apr 13 '23 at 03:25

1 Answers1

0

Your 'example.exe' application may not writing any output. Here is a test I threw together to prove your posted code works if the 'example.exe' application is sending output.


The main program to start process and capture messages. This is a copy of the code you posted, nothing different about it.

internal class Program
{
    static void Main(string[] args)
    {
        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();
    }
}

A quick test example.exe application to capture args and repeatedly write output.

internal class Program
{
    static void Main(string[] args)
    {

        if (args.Length > 0 && args[0] != null)
        {
            Console.Write($"Argument received: {args[0]}");
        }
        if (args.Length > 1 && args[1] != null)
        {
            Console.WriteLine($" {args[1]}");
        }

        while (true)
        {
            Console.WriteLine($"DateTime: {DateTime.Now}");
            Task.Delay(1000).Wait();
        }
    }
}

Output from the main program's console window.

Input file name: abcd.txt
Argument received: --file abcd.txt
DateTime: 4 / 12 / 2023 5:45:10 PM
DateTime: 4 / 12 / 2023 5:45:11 PM
DateTime: 4 / 12 / 2023 5:45:12 PM
DateTime: 4 / 12 / 2023 5:45:13 PM
DateTime: 4 / 12 / 2023 5:45:14 PM
DateTime: 4 / 12 / 2023 5:45:15 PM
DateTime: 4 / 12 / 2023 5:45:16 PM
quaabaam
  • 1,808
  • 1
  • 7
  • 15
  • The example program works, but the one I'm trying to use doesn't. That's weird, because the one I'm trying to use works fine in the command prompt, live output and everything. – TheTank20 Apr 13 '23 at 01:08