2015-08-08 41 views
1

在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() // ? 
    } 
} 

回答

1

你知道有多少字节已经从原始流通过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); 

,然后该方法可以利用读取块的数据关怀秒。

+0

谢谢!这让我更了解了一些! –