STDIN/STDOUT 是一次性刷新数据还是逐个字符刷新数据?
我正在使用 C# 读取另一个程序的 STDOUT。如果我这样做:
StreamReader reader = process.StandardOutput;
reader.ReadToEnd();
是否可以保证将最后一个内容完整地刷新到程序的 STDOUT 中?或者有点像 TCP,我必须有一个消息终止符或长度标头?
I'm using C# to read another program's STDOUT. If I do this:
StreamReader reader = process.StandardOutput;
reader.ReadToEnd();
Is it guaranteed to get the last thing flushed out to the program's STDOUT in its entirety? Or is kind of like TCP where I'd have to have a message terminator or length header?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,StandardOutput 是缓冲的,这意味着您可能会从另一端获取完整消息(或多个完整消息)。但它并不能真正保证,特别是因为您正在读取的进程可能会更改标准输出的缓冲。
消息终止符将是解决这个问题的最佳方法。尽管通常通过 StandardOutput 进行通信的进程一切都是基于行的,因此简单地使用换行符作为消息终止符可能是最简单且最常见的起点。
by default StandardOutput is buffered, which means that you would likely get whole messages from the other end (or multiple whole messages). But its not really guaranteed, especially because process you are reading from could have changed the buffering of StandardOutput.
A message terminator would be the best way to figure it out. though usually with processes communicating over StandardOutput everything is line based so simply using newlines as message terminators is probably the simplest and most common place to start.
reader.ReadToEnd() 在进程终止之前不会返回,因此在调用之后您应该看到它写入 stdout 的所有内容。缓冲只会影响它从其他程序进入阅读器缓冲区的速度,但您的代码无法区分差异(至少在这个线程上),因为它仍在等待
ReadToEnd()
返回。reader.ReadToEnd()
doesn't return until the process terminates, so after that call you should see everything that it wrote to stdout. Buffering would only affect how quickly it gets from the other program into your reader's buffer, but your code can't tell the difference (at least on this thread) because it's still waiting forReadToEnd()
to return.