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)

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


Attachments

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!

Note You need to log in before you can comment on or make changes to this bug.