博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Process.StandardOutput
阅读量:4941 次
发布时间:2019-06-11

本文共 6999 字,大约阅读时间需要 23 分钟。

Namespace:  

Assembly:  System (in System.dll)

 
C#
 
[BrowsableAttribute(false)]public StreamReader StandardOutput { get; }

Property Value

Type: 
A   that can be used to read the standard output stream of the application.

 
Exception Condition

The StandardOutput stream has not been defined for redirection; ensure  is set to trueand  is set to false.

- or -

The StandardOutput stream has been opened for asynchronous read operations with .

 

When a  writes text to its standard stream, that text is normally displayed on the console. By redirecting the StandardOutput stream, you can manipulate or suppress the output of a process. For example, you can filter the text, format it differently, or write the output to both the console and a designated log file.

NoteNote

To use StandardOutput, you must set  to false, and you must set  to true. Otherwise, reading from the StandardOutput stream throws an exception.

The redirected StandardOutput stream can be read synchronously or asynchronously. Methods such as , , and  perform synchronous read operations on the output stream of the process. These synchronous read operations do not complete until the associated  writes to its StandardOutputstream, or closes the stream.

In contrast,  starts asynchronous read operations on the StandardOutput stream. This method enables a designated event handler for the stream output and immediately returns to the caller, which can perform other work while the stream output is directed to the event handler.

Synchronous read operations introduce a dependency between the caller reading from the StandardOutput stream and the child process writing to that stream. These dependencies can result in deadlock conditions. When the caller reads from the redirected stream of a child process, it is dependent on the child. The caller waits on the read operation until the child writes to the stream or closes the stream. When the child process writes enough data to fill its redirected stream, it is dependent on the parent. The child process waits on the next write operation until the parent reads from the full stream or closes the stream. The deadlock condition results when the caller and child process wait on each other to complete an operation, and neither can proceed. You can avoid deadlocks by evaluating dependencies between the caller and child process.

The following C# code, for example, shows how to read from a redirected stream and wait for the child process to exit.

 
 
// Start the child process. Process p = new Process(); // Redirect the output stream of the child process. p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.FileName = "Write500Lines.exe"; p.Start(); // Do not wait for the child process to exit before // reading to the end of its redirected stream. // p.WaitForExit(); // Read the output stream first and then wait. string output = p.StandardOutput.ReadToEnd(); p.WaitForExit();

The code example avoids a deadlock condition by calling p.StandardOutput.ReadToEnd before p.WaitForExit. A deadlock condition can result if the parent process calls p.WaitForExit before p.StandardOutput.ReadToEnd and the child process writes enough text to fill the redirected stream. The parent process would wait indefinitely for the child process to exit. The child process would wait indefinitely for the parent to read from the full StandardOutput stream.

There is a similar issue when you read all text from both the standard output and standard error streams. The following C# code, for example, performs a read operation on both streams.

 
 
// Do not perform a synchronous read to the end of both  // redirected streams. // string output = p.StandardOutput.ReadToEnd(); // string error = p.StandardError.ReadToEnd(); // p.WaitForExit(); // Use asynchronous read operations on at least one of the streams. p.BeginOutputReadLine(); string error = p.StandardError.ReadToEnd(); p.WaitForExit();

The code example avoids the deadlock condition by performing asynchronous read operations on the StandardOutput stream. A deadlock condition results if the parent process calls p.StandardOutput.ReadToEnd followed by p.StandardError.ReadToEnd and the child process writes enough text to fill its error stream. The parent process would wait indefinitely for the child process to close its StandardOutput stream. The child process would wait indefinitely for the parent to read from the full  stream.

You can use asynchronous read operations to avoid these dependencies and their deadlock potential. Alternately, you can avoid the deadlock condition by creating two threads and reading the output of each stream on a separate thread.

NoteNote

You cannot mix asynchronous and synchronous read operations on a redirected stream. Once the redirected stream of a  is opened in either asynchronous or synchronous mode, all further read operations on that stream must be in the same mode. For example, do not follow with a call to  on the StandardOutput stream, or vice versa. However, you can read two different streams in different modes. For example, you can call and then call  for the  stream.

 

The following example spawns a new user-defined executable and reads its standard output. The output is then displayed in the console.

C#
 
using System;using System.IO;using System.Diagnostics;class IORedirExample{    public static void Main()    {        string[] args = Environment.GetCommandLineArgs();        if (args.Length > 1)        {            // This is the code for the spawned process            Console.WriteLine("Hello from the redirected process!");        }        else        {            // This is the code for the base process            Process myProcess = new Process();            // Start a new instance of this program but specify the 'spawned' version.            ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(args[0], "spawn");            myProcessStartInfo.UseShellExecute = false;            myProcessStartInfo.RedirectStandardOutput = true;            myProcess.StartInfo = myProcessStartInfo;            myProcess.Start();            StreamReader myStreamReader = myProcess.StandardOutput;            // Read the standard output of the spawned process.             string myString = myStreamReader.ReadLine();            Console.WriteLine(myString);            myProcess.WaitForExit();            myProcess.Close();        }    }}

 

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

 
  •  

    for full trust for the immediate caller. This member cannot be used by partially trusted code.

 

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .

 

Reference

转载于:https://www.cnblogs.com/itelite/p/4063150.html

你可能感兴趣的文章
spider_使用parse,urlencode,爬取豆瓣电影(get请求拼接url)
查看>>
Debian安装中文输入法
查看>>
GMA Round 1 最短距离
查看>>
HTML5 Canvas 超炫酷烟花绽放动画教程
查看>>
sklearn——数据集调用及应用
查看>>
设置span的宽度
查看>>
234. Palindrome Linked List
查看>>
linux部分命令的全称
查看>>
CSS 制作的导航菜单
查看>>
libaio.so.1()(64bit) is needed by MySQL-server 问题解决办法
查看>>
概率论与数理统计中基于有限样本推断总体分布的方法,基于总体未知参数区间估计的假设检验方法之讨论,以及从数理统计视角重新审视线性回归函数本质...
查看>>
洛谷P1111 修复公路
查看>>
图论存图方式小结
查看>>
购物车的实现原理
查看>>
XML序列化的注意事项
查看>>
POJ 3104 二分
查看>>
bzoj 3874: [Ahoi2014]宅男计划
查看>>
Linux上的SQL Server的起步
查看>>
AFNetworking自带的解析图片的方法
查看>>
Apache与Nginx的区分比较
查看>>