用CreatePipe创建的匿名管道,如何异步"读取"?

oiiiip 2011-10-31 10:42:43
如题,我创建一个匿名管道,一个线程WritePipe,一个ReadPipe。我希望ReadPipe的线程,不要阻塞等待而是采用异步的方式,如何做到。

CreateFileEx可以创建异步的文件句柄,问题是,管道的哦句柄已经通过CreatePipe来创建了,可以通过什么api来修改这个文件句柄的属性,使他支持异步模式么?
...全文
574 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
向立天 2011-11-01
  • 打赏
  • 举报
回复
匿名管道不支持异步读写
参考这篇文章
http://hi.baidu.com/jk_cau/blog/item/486c38c84412d21a7e3e6f2d.html
oiiiip 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hztj2005 的回复:]

一个线程WritePipe,一个ReadPipe。我希望ReadPipe的线程,不要阻塞等待而是采用异步的方式,如何做到。

不很理解你的要求,通常是开启两个管道,一个由A写,B读,一个B写,A读。你说的异步是什么?
[/Quote]

我的要求是,"读线程"不是用阻塞的方式循环等待读取,而是用异步响应函数的方式读取。可是这要求CreateFile的时候指定参数。
问题是句柄是用CreatePipe创建的而非CreateFile,这个异步overlap的参数我无处指定啊。

我该如何做到?
Gloveing 2011-10-31
  • 打赏
  • 举报
回复
MSDN的例子

// set up overlapped structure fields
gOverLapped.Offset = 0;
gOverLapped.OffsetHigh = 0;
gOverLapped.hEvent = hEvent;

// attempt an asynchronous read operation
bResult = ReadFile(hFile, &inBuffer, nBytesToRead, &nBytesRead,
&gOverlapped) ;


// if there was a problem, or the async. operation's still pending ...
if (!bResult)
{
// deal with the error code
switch (dwError = GetLastError())
{
case ERROR_HANDLE_EOF:
{
// we're reached the end of the file
// during the call to ReadFile

// code to handle that
}

case ERROR_IO_PENDING:
{
// asynchronous i/o is still in progress

// do something else for a while
GoDoSomethingElse() ;

// check on the results of the asynchronous read
bResult = GetOverlappedResult(hFile, &gOverlapped,
&nBytesRead, FALSE) ;

// if there was a problem ...
if (!bResult)
{
// deal with the error code
switch (dwError = GetLastError())
{
case ERROR_HANDLE_EOF:
{
// we're reached the end of the file
//during asynchronous operation
}

// deal with other error cases
}
}
} // end case

// deal with other error cases

} // end switch
} // end if
hztj2005 2011-10-31
  • 打赏
  • 举报
回复
一个线程WritePipe,一个ReadPipe。我希望ReadPipe的线程,不要阻塞等待而是采用异步的方式,如何做到。

不很理解你的要求,通常是开启两个管道,一个由A写,B读,一个B写,A读。你说的异步是什么?

oiiiip 2011-10-31
  • 打赏
  • 举报
回复
我的要求是匿名管道的异步通信啊

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧