如何从子进程的STDOUT接收输出而不阻塞或Poling

I have a long-running console-based application Sender that sends simple text to STDOUT using non-buffered output such as cout << "Message" << flush(). I want to create an MFC dialog-based application (named Receiver) that starts Sender and can read it's output. Receiver should also be able to detect when Sender has died, or be able to kill Sender if it wants to. Sender knows nothing of Reciever, and I can't change Sender's code.

My first attempt was to create pipes with redirected STDIN and STDOUT for the child process and use asynchronous ReadFileEx calls to read in Sender's data. This isn't working correctly, and I've posted a separate thread about those specific problems.

My question is, how should I be doing this, in general architectural terms? I don't want Receiver's main loop to block or poll, but should use some flavor of Wait function.

#0

You have 2 basic options. Option 1 you've already tried, doing asynchronous (aka nonblocking) IO to read/write from the child process. Option 2 is to create a separate thread in the Receiver process that does blocking reads/writes from/to the child process.

I'd recommend option 2, I find it much easier. You then, of course, have the problem of how to get the data from the helper thread to the main thread. You'll need to use locks and maybe semaphores for that. It should be less of a hassle than nonblocking IO, however.

推荐文章

SQL Server中的列名无效

SQL Server中的列名无效

推荐文章

在Erlang中,record_info和tuple_to_list是否返回相同的键顺序?

在Erlang中,record_info和tuple_to_list是否返回相同的键顺序?

推荐文章

小马不发送电子邮件到gmail地址?

小马不发送电子邮件到gmail地址?

推荐文章

实时更新Gridview

实时更新Gridview

推荐文章

Razor与ASPX中的迭代

Razor与ASPX中的迭代

推荐文章

有没有什么方法可以从浏览器运行服务器或点对点?

有没有什么方法可以从浏览器运行服务器或点对点?

推荐文章

向列表中的每个列表项添加图标

向列表中的每个列表项添加图标

推荐文章

zend 2中的ZendX_JQuery是否关闭

zend 2中的ZendX_JQuery是否关闭

推荐文章

绘制超过2个因子

绘制超过2个因子

推荐文章

iPad中的JqueryMobile“Go”按钮

iPad中的JqueryMobile“Go”按钮

推荐文章

超文本视图内容大小.高度比需要的大?

超文本视图内容大小.高度比需要的大?

推荐文章

JAI图像调整瓶颈

JAI图像调整瓶颈

推荐文章

可为空布尔值的复选框

可为空布尔值的复选框

推荐文章

谷歌places能否返回地图上显示的企业网站和电子邮件地址?

谷歌places能否返回地图上显示的企业网站和电子邮件地址?

推荐文章

使用Microsoft Visual Studio 8在boost编译中生成BJAM 1.33.1

使用Microsoft Visual Studio 8在boost编译中生成BJAM 1.33.1

推荐文章

PHP 5.3.5-1连接到本地主机上的MySQL数据库,Ubuntu 11.04

PHP 5.3.5-1连接到本地主机上的MySQL数据库,Ubuntu 11.04