Export (0) Print
Expand All

ProcessStartInfo.RedirectStandardError Property

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

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

Property Value

true to write error output to Process.StandardError; 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.

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 | StandardError

Show:
© 2014 Microsoft