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.