逐块发送流
send a stream chunk by chunk
我对 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);
然后该方法可以分块读取数据。
我对 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);
然后该方法可以分块读取数据。