Export (0) Print
Expand All
1 out of 5 rated this helpful - Rate this topic

ProcessStartInfo.RedirectStandardOutput Property

Gets or sets a value indicating whether the process output is written to the Process instance's StandardOutput member, enabling you to write to a destination other than the standard output stream (usually the monitor screen). Used to write data to a file, for example.

[Visual Basic]
Public Property RedirectStandardOutput As Boolean
[C#]
public bool RedirectStandardOutput {get; set;}
[C++]
public: __property bool get_RedirectStandardOutput();
public: __property void set_RedirectStandardOutput(bool);
[JScript]
public function get RedirectStandardOutput() : Boolean;
public function set RedirectStandardOutput(Boolean);

Property Value

true to write output to Process.StandardOutput; otherwise, false.

Remarks

The Process component communicates with a child process using a pipe. If a child process writes enough data to the pipe to fill the buffer, the child will block until the parent reads the data from the pipe. This can cause deadlock if your application is reading all output to standard error and standard output, for example, using the following C# code.

[C#] 
ProcessStartInfo ps = new ProcessStartInfo("...", "...");
ps.UseShellExecute = false;
ps.RedirectStandardOutput = true;
Process p=new Process();
p.Start(ps);
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();

In this instance, both the parent and the child processes would be blocked, as the filled pipe prevents the child process from completing, while the parent process is waiting indefinitely for the child process to exit.

This problem can be solved by moving the ReadToEnd() before the WaitForExit(), as follows.

[C#] 
ProcessStartInfo ps = new ProcessStartInfo("...", "...");
ps.UseShellExecute = false;
ps.RedirectStandardOutput = true;
Process p=new Process();
p.Start(ps);
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

A similar problem arises if you redirect both standard output and standard error and then try to read both, for example using the following C# code.

[C#] 
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
p.WaitForExit();

In this case, if the child process writes any text to standard error it will block the process, because the parent process cannot read from standard error until it has finished reading from standard output. However, the parent process will not read from standard output until the process ends. A recommended solution to this situation is to create two threads so that your application can read the output of each stream on a separate thread.

Example

[Visual Basic] 
compiler.StartInfo.FileName = "csc.exe"
compiler.StartInfo.Arguments = "/r:System.dll /out:sample.exe stdstr.cs"
compiler.StartInfo.UseShellExecute = False
compiler.StartInfo.RedirectStandardOutput = True
compiler.Start()

Console.WriteLine(compiler.StandardOutput.ReadToEnd())

compiler.WaitForExit()

[C#] 
Process compiler = new Process();
compiler.StartInfo.FileName = "csc.exe";
compiler.StartInfo.Arguments = "/r:System.dll /out:sample.exe stdstr.cs";
compiler.StartInfo.UseShellExecute = false;
compiler.StartInfo.RedirectStandardOutput = true;
compiler.Start();    

Console.WriteLine(compiler.StandardOutput.ReadToEnd());

compiler.WaitForExit();

[C++] 
Process* compiler = new Process();
compiler->StartInfo->FileName = S"cl.exe";
compiler->StartInfo->Arguments = S"/clr stdstr.cpp /link /out:sample.exe";
compiler->StartInfo->UseShellExecute = false;
compiler->StartInfo->RedirectStandardOutput = true;
compiler->Start();

Console::WriteLine(compiler->StandardOutput->ReadToEnd());

compiler->WaitForExit();

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

ProcessStartInfo Class | ProcessStartInfo Members | System.Diagnostics Namespace | UseShellExecute | StandardOutput

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.