禁用重定向标准输出管道上的缓冲(Win32 API,C++) [英] Disable buffering on redirected stdout Pipe (Win32 API, C++)

查看:44
本文介绍了禁用重定向标准输出管道上的缓冲(Win32 API,C++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 CreateProcess 从 Win32 生成一个进程,设置 STARTUPINFOhStdOutputhStdError 属性到用 CreatePipe 创建的管道句柄.我有两个线程正在读取管道,等待数据变为可用(或进程完成,此时它会在终止线程之前检查是否没有剩余数据).
当数据可用时,我将输出写出到一个大文本框.

I'm spawning a process from Win32 using CreateProcess, setting the hStdOutput and hStdError properties of STARTUPINFO to pipe handles created with CreatePipe. I've got two threads reading the pipes, waiting for data to become available (or the process to complete, at which point it checks that there is no data left before terminating the thread).
As data becomes available, I write the output out to effectively a big textbox.

正在发生的事情是输出被缓冲,因此运行缓慢的进程只会将大量数据抛出到文本框,而不是发生时".

What's happening is the output is being buffered, so a slow running process just gets chunks of data thrown at the text box, but not "as it happens".

我不确定是管道在做缓冲,还是与重定向有关.

I'm not sure if it's the pipe that's doing the buffering, or something to do with the redirection.

有没有办法将管道设置为无缓冲,或者以尽快发送标准输出的方式启动进程?

Is there any way to either set the pipe to be unbuffered, or start the process in such a way that the stdout is sent as soon as possible?

我正在使用一个测试应用程序进行测试,该应用程序将打印行间隔一秒

I'm testing with a test app that prints lines one second apart

Here is line one
(waits one second)
Here is line two
(waits one second)
... etc

推荐答案

缓冲可能在 C 运行时(printf 等)中,对此您无能为力(IIRC 它执行 isatty() 检查以确定一种缓冲策略)

The buffering is probably in the C runtime (printf etc) and there is not much you can do about it (IIRC it does a isatty() check to determine a buffering strategy)

这篇关于禁用重定向标准输出管道上的缓冲(Win32 API,C++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆