0

I have found many similar questions to this but non of the answers work quite as I desire and I think the situations were a bit different.

A bit of background: basically I created a C# command line application and I want to create a GUI version of that. My thinking was that the user can select various options that will configure the arguments that are passed to the CLI then I will use Process to start the CLI. And I want to display the output from the CLI into a ListBox on my Form in real time. I have tried a few different solutions provided in different questions: 1, 2, 3.

The closest thing I have at this point looks something like this:

ProcessStartInfo info = new ProcessStartInfo();
info.CreateNoWindow = true;
info.UseShellExecute = false;
info.WindowStyle = ProcessWindowStyle.Hidden;
// FileName and Arguments are specific to my CLI
info.FileName = "SimSwitcher.exe";
info.Arguments = String.Format("{0} -d {1} -f1 {2} -f2 {3} -s {4} -l {5}", command.ToString(), device.ToString(), file1Path, file2Path,
            Convert.ToString(this.startingAddress, 16), Convert.ToString(this.length, 16));
info.RedirectStandardOutput = true;
proc = new Process();
proc.StartInfo = info;
proc.EnableRaisingEvents = true;
proc.OutputDataReceived += Proc_OutputDataReceived;
proc.Start();
proc.BeginOutputReadLine();

Then this is my event handler:

private void Proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    if (e.Data != null)
    {
        this.BeginInvoke(new MethodInvoker(() => { outputBox.Items.Add(e.Data);}));
    }
}

Where outputBox is the ListBox.

Right now this is close to what I want, but the output is not quite added in real time. Within the code for my SimSwitcher.exe CLI, I am using Process again to run three other CLI's in succession (that I didn't develop). What happens is that the output of the first process is not added to the ListBox until the first process is finished and likewise for the second and third processes. I tried using similar code as above to start those three processes in my SimSwitcher.exe CLI but that didn't help and it actually caused the SimSwitcher.exe CLI to not display the output in real time when it was run from the command line (it did display the output in real time before that change).

My suspicion is that there's nothing I can do to fix this and it's something with the the 3 CLI processes that my CLI is running. But maybe I am missing something, any feedback or suggestions would be appreciated.

frankM_DN
  • 365
  • 7
  • Since te code you have posted just shows what your `SimSwitcher.exe` outputs, you should probably post that code instead, including the modified part that causes the program to stop writing to the Console (it could be more interesting to fix) – Jimi Oct 12 '22 at 18:35
  • It sounds like your problem is not actually getting console output, but when your UI is notified that output has been received. Are you using tasks to start the process? Are you running them sequentially or in parallel, and is this happening on a separate thread from your UI? Looks like you are in code behind instead of a MVVM pattern that will further muddy the waters. If you are running the process as a separate thread (to free up UI updates) you will have to handle output on UI thread. – Anthony G. Oct 12 '22 at 18:36
  • @AnthonyG. currently I am running the process from the same thread as the UI. But the `Proc_OutputDataReceived` event handler creates a new thread to avoid exceptions with the UI. I tried creating another thread and running the process there but that didn't help. The 3 processes in `SimSwitcher.exe` are run sequentially and they need to be since the 3rd one relies on files that the 1st one creates. But yes, that assessment seems correct that the UI isn't getting notified at the proper time. – frankM_DN Oct 12 '22 at 18:46
  • @Jimi I'm not positive what you mean but the `SimSwitcher.exe` uses the same logic to create the process as the code I posted except it doesn't redirect `StandardOutput` and when I changed it to redirect `StandardOutput` it stopped writing to the console. – frankM_DN Oct 12 '22 at 18:48
  • Your executable that acts here as *mediator* is responsible for receiving the input from those other applications and *distributing* their outputs. If you simply redirect stdout, you of course don't write to the default stdout (what you see in the Console window). Then it depends on what and how your Console app writes out. Did you try to write to stderr? The GUI is the final *receiver*, it appears not relevant here – Jimi Oct 12 '22 at 19:21
  • Well the GUI receives all of the output it just doesn't receive it in real time. So I'm not sure how to distribute the output from the applications differently. Because that output is generated by those applications themselves while they are running, not by anything that I do. – frankM_DN Oct 12 '22 at 19:37

0 Answers0