Bug 7584 - System.Diagnostics.Process does not work standard output redirection when you restart the process
Summary: System.Diagnostics.Process does not work standard output redirection when you...
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System (show other bugs)
Version: 2.8.x
Hardware: All All
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-10-02 07:57 UTC by Egor
Modified: 2012-10-02 07:57 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 7584 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
NEW

Description Egor 2012-10-02 07:57:15 UTC
Description:
------------
Does not work standard output redirection when you restart the process.

Steps to reproduce the issue:
-----------------------------
1. Create the following console application and run it.
// Main.cs
using System;
using System.Diagnostics;

namespace TestProcess
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			Process benchmark = new Process ();

			// Here you should specify any application that prints a message to the standard output stream.
			benchmark.StartInfo.FileName = "HelloWorld.exe";
			benchmark.StartInfo.RedirectStandardOutput = true;
			benchmark.StartInfo.RedirectStandardError = true;
			benchmark.StartInfo.UseShellExecute = false;

			DataReceivedEventHandler stdoutHandler = new DataReceivedEventHandler(StdOutputHandler);
			DataReceivedEventHandler erroutHandler = new DataReceivedEventHandler(ErrorOutputHandler);

			for (int i = 0; i < 10; ++i) {
				Console.WriteLine("------- Iteration {0} -------", i + 1);
				benchmark.OutputDataReceived += stdoutHandler;
				benchmark.ErrorDataReceived += erroutHandler;

				benchmark.Start();

				benchmark.BeginOutputReadLine();
				benchmark.BeginErrorReadLine();

				benchmark.WaitForExit();

				benchmark.CancelErrorRead();
				benchmark.CancelOutputRead();
				
				benchmark.OutputDataReceived -= stdoutHandler;
				benchmark.ErrorDataReceived -= erroutHandler;
			}
		}

		private static void StdOutputHandler(object sendingProcess,
		                              DataReceivedEventArgs outLine)
		{
			if (outLine.Data != null)
			{
				Console.WriteLine("Benchmark Output: {0}", outLine.Data);
			}
		}
		
		private static void ErrorOutputHandler(object sendingProcess,
		                                DataReceivedEventArgs outLine)
		{
			if (outLine.Data != null)
			{
				Console.WriteLine("Benchmark Error Output: {0}", outLine.Data);
			}
		}
	}
}

2. Inspect application output. You will see the following:
------- Iteration 1 -------
Hello world!
------- Iteration 2 -------
------- Iteration 3 -------
------- Iteration 4 -------
------- Iteration 5 -------
------- Iteration 6 -------
------- Iteration 7 -------
------- Iteration 8 -------
------- Iteration 9 -------
------- Iteration 10 -------

Note:
-----
I tested this code on .Net, it works correctly. And I see in the application output the following:
------- Iteration 1 -------
Hello world!
------- Iteration 2 -------
Hello world!
------- Iteration 3 -------
Hello world!
------- Iteration 4 -------
Hello world!
------- Iteration 5 -------
Hello world!
------- Iteration 6 -------
Hello world!
------- Iteration 7 -------
Hello world!
------- Iteration 8 -------
Hello world!
------- Iteration 9 -------
Hello world!
------- Iteration 10 -------
Hello world!