繁体   English   中英

Process.StandardOutput流是如何存储的?

[英]How are Process.StandardOutput streams stored?

我是Streams的新手,我正在尝试编写一个函数来重定向Process的StandardOutput并返回StandardOutput Stream,以便它可以在其他地方使用。 当我在方法中返回Process.StandardOutput Stream时,该Stream是如何存储的? 它是否只是在消耗之前存在于某个地方? 它有最大尺寸吗?

这是我到目前为止的一些示例代码:

    public Stream GetStdOut(string file)
    {
        _process = new Process(file);
        _process.StartInfo.UseShellExecute = false;
        _process.StartInfo.RedirectStandardOutput = true;
        _process.Start();
        return _process.StandardOutput;
    }

    public bool CompareStreams()
    {
        Stream s1 = GetStdOut("somefile.exe");
        Stream s2 = GetStdOut("anotherfile.exe");

        using (StreamReader sr1 = new StreamReader(s1))
        using (StreamReader sr2 = new StreamReader(s2))
        {
            string line_a, line_b;
            while ((line_a = sr1.ReadLine()) != null &&
                   (line_b = sr2.ReadLine()) != null)
            {
                if (line_a != line_b)
                    return false;
            }
            return true;
        }
    }

那么在CompareStreams()中,当我为Stream s2生成数据时,是否需要担心与Stream s1相关的数据量? 或者这不重要吗?

Stream对象不会立即填充所有标准输出数据。

当进程运行时,它们会写入缓冲区,当缓冲区已满时,它们将冻结,直到您从缓冲区读取为止。

当您从缓冲区中读取时,会清除进程以写入更多数据的空间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM