-1

Trying to use Process class and it's async features. Couldn't figure out how to read all output from Process before the program exits. Please help!!

Here's my code,

void RunProcess()
{
  Process process = new Process
  {
      StartInfo = new ProcessStartInfo
      {
          FileName = CmdName,
          Arguments = CmdArgs,
          CreateNoWindow = true,
          WindowStyle = ProcessWindowStyle.Hidden,
          RedirectStandardOutput = true,
          RedirectStandardError = true,
          UseShellExecute = false
      }
  };

  using(process)
  {
      process.OutputDataReceived += Process_OutputDataReceived;

      process.Start();
      process.BeginOutputReadLine();

      process.WaitForExit(); // Even waiting for exit here.

      _logger.Debug("End of process");
  }
} // void RunProcess()

void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
  if (!String.IsNullOrEmpty(e.Data))
      _logger.Debug($"\t{e.Data}");
}

I have a other code that runs for at least another 30 - 45 secs after RunProcess() method is called and done, but don't see the output from my process anywhere in the logs.

If I run the program synchronously, I get all the output. But see no output when run async. Does anyone know what I am doing wrong, please?

(Updating question to make it more clear!)

The code I posted above works, and is minimal ( stripped out validations, classes, etc). I am looking for suggestions on how to make my program stop until full output is captured in log files. Does anyone know if there is a way to make it happen with the combination of WaitForExit and event call like I have in code above, please? It seems the process is completing first and terminating the event handler before it could print the log lines.

Many Thanks in advance!!

scorpion35
  • 944
  • 2
  • 12
  • 30
  • How is it that you started a new process synchronously? A new process is a separate process from the current one. I see that you setup a `WaitForExit` block. Is that what you are referring to when you say synch/async? – P.Brian.Mackey Apr 30 '18 at 17:25
  • @P.Brian.Mackey yes. I meant async from the `Process_OutputDataReceived` event. I am under the impression that the event should print all data because of `WaitForExit`, before moving onto next line. Apparently, I am missing something; will look into `async/await` below. – scorpion35 Apr 30 '18 at 17:36
  • @downvoter Could you explain why? – scorpion35 May 01 '18 at 14:19

1 Answers1

0

You can organize your code a little better. Make some class to contain your process. Take a look at how to do async/await.

Remove the line below and read the output from the process its self instead of delegating it.

  process.OutputDataReceived += Process_OutputDataReceived;

How and When to use `async` and `await`

Zakk Diaz
  • 1,063
  • 10
  • 15
  • Additionally this post has information on how to use the delegate https://stackoverflow.com/questions/50105158/c-sharp-process-async-read-all-output/50105201#50105201 – Zakk Diaz Apr 30 '18 at 17:19
  • Thank you! I will look into `async/await` now. – scorpion35 Apr 30 '18 at 17:32