1

I have a C# Console application which I am trying to execute from another WinForm application just like batch runner by giving the console application's .exe file like below.

Process.Start("Path of Console application exe to execute")

However I need to wait and handle the output and display the output in WinForm's richtextbox from console application once it has completed the execution. How can I achieve this?

Update

I have changed the code to Start a Process and Read using StandardOutput and BeginOutputReadLine() to Read the output asynchronously, but not able to see output in console window, instead console window is getting closed. Not sure how to solve this.

            p.StartInfo.UseShellExecute = false;
            // p.StartInfo.CreateNoWindow = True
            p.StartInfo.RedirectStandardOutput = true;

            string @out = null;

            p.StartInfo.RedirectStandardError = true;

            p.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
            {
                @out += e.Data;
            });
            p.StartInfo.FileName = currentTest;

            p.Start();

            p.BeginOutputReadLine();

            // string output = p.StandardError.ReadToEnd();
            // p.WaitForExit()

            while (!p.HasExited)
                Application.DoEvents();

            //Console.WriteLine($@"Output \n'{output.Substring(output.Length - 50)}'");
            Console.WriteLine($@"\n Error stream:  {@out}");

            Console.ReadLine();
harsha.cs
  • 122
  • 2
  • 13
  • Do you own the code of that Console App? – Fildor Sep 21 '21 at 07:43
  • 3
    If you need the output, you need to [redirect StdOut](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.redirectstandardoutput?view=net-5.0). If you need wait for the Process to finish, you can [do so](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.waitforexit?view=net-5.0). – Fildor Sep 21 '21 at 07:48
  • 3
    But if the answer to my 1. question is "yes", it's probably better to factor out the "business logic" into a library and use that instead of out-of-process stuff. – Fildor Sep 21 '21 at 07:50
  • @Fildor: Yes I own both Console and WinForm applications. Will try out your suggestions, thank you. – harsha.cs Sep 21 '21 at 08:48
  • @Fildor: I have modified my code with Redirect StdOut, but still not able to see anything on Console instead Console is closed when error occurs in Console application. How to proceed? And can you give me brief hint on "business logic" you said above. – harsha.cs Sep 21 '21 at 18:26

1 Answers1

1

You need to redirect stdout (and probably stderr) so that any output comes to you, instead of a console; you may also want to redirect stdin. All of these things are available via ProcessStartInfo, with an example on MSDN. Note that if you want to display updates while the exe is running, you may need a worker thread to read incrementally from StandardOutput and StandardError, rather than ReadToEnd() - which won't return anything at all until the associated output pipe is closed.

However! If the console exe is "yours", it may be simpler to just expose the functionality you want in a library, and invoke it directly in-process. There are times when out-of-process is actively preferred, such as when you need to allow that process to go catastrophically wrong in some scenarios - but usually in-process is preferable, given free rein.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Marc: I have updated my answer by using ``StandardOutput`` and ``BeginOutputReadLine``. But I cant see the output in console instead console is closing in middle when error occurs in Console app. What is wrong in my code? And can you give me hint on "in-process" you said above? – harsha.cs Sep 21 '21 at 18:28