C# Run a long CMD process with live output on form

Mr Cheese 1 Reputation point
2020-08-15T01:29:12.91+00:00

I am sure this question is answered out there somewhere, but I am not having much success with this.
I am seeking to have a long CMD.exe process to run, and show the live output to a Windows Form.
All the solutions I am finding all seem to have to wait for the process to complete, and then output is redirected text at the end.
I am not having any luck working out how to show the progress while it is running, and being notified when completed.
I have tried many variations, but not having any luck yet.
So simply looking to run this code below, and see progress on the WinForm, and then raise an event when the command is completed.

Process process = new Process();
process.StartInfo.FileName="cmd.exe";
process.StartInfo.Arguments=@"/C DIR C:\ /s";

I found a working solution using SetParent, MoveWindow, SetWindowLong functions from user32.dll, but I can't control the CMD Windows appearance.

Looking for a simple (elegant) way to achieve this result so I can see the progress, and control the position, fonts and colours as well.

Again, I know solutions are around, but finding this more complicated than what I would like.
Thanks for any suggestions.

Community Center Not monitored
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 90,521 Reputation points
    2020-08-15T12:28:41.417+00:00

    You can use a BackgroundWorker
    For example, this adds the result of the command in a RichTextBox richTextBox1 =>

    Pfff, I cannot post code on those bugged forums, so I post a jpg of the code I tested =>
    (change C:\Temp, that I used to check when it was finished )

    asynccode.jpg

    3 people found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.