逐块发送流

本文关键字: | 更新日期: 2023-09-27 18:06:54

说到c#中的流,我是一个新手,但我对基础知识有点熟悉。

我需要帮助设置钩入未知长度流的最有效方式,并将读取的部分发送到另一个函数,直到到达流的末尾。有人可以看看我有什么,并帮助我填写部分在while循环,或者如果while循环不是最好的方式告诉我什么是更好的。如有任何帮助,不胜感激。

var processStartInfo = new ProcessStartInfo
{
    FileName = "program.exe",
    RedirectStandardInput = true,
    RedirectStandardOutput = true,
    UseShellExecute = false,
    CreateNoWindow = true,
    Arguments = " -some -arguments"
};
theProcess.StartInfo = processStartInfo;
theProcess.Start();
while (!theProcess.HasExited)
{
    int count = 0;
    var b = new byte[32768]; // 32k
    while ((count = theProcess.StandardOutput.BaseStream.Read(b, 0, b.Length)) > 0)
    {
        SendChunck() // ?
    }
}

逐块发送流

您知道通过count变量从原始流中读取了多少字节,因此您可以将它们放入缓冲区

while ((count = theProcess.StandardOutput.BaseStream.Read(b, 0, b.Length)) > 0)
{
    byte[] actual = b.Take(count).ToArray();
    SendChunck(actual);
}

或者如果您的SendChunk方法被设计为以Stream作为参数,您可以直接将原始对象传递给它:

SendChunck(theProcess.StandardOutput.BaseStream);

,然后该方法可以负责读取数据块

相关文章:
  • 没有找到相关文章