OVERLAPPED Win32 结构中的 hEvent 成员 [英] hEvent member in OVERLAPPED Win32 structure

查看:34
本文介绍了OVERLAPPED Win32 结构中的 hEvent 成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用异步 I/O(或 Win32 行话中的重叠"I/O)时,我们需要处理 OVERLAPPED 结构和他的 hEvent 成员.如果 I/O 函数会延迟读或写操作,我们将得到一个 ERROR_IO_PENDING 错误代码,然后我们将使用 WaitForXxxEvent 函数等待异步操作完成,然后我们将调用GetOverlappedResult.

When asynchronous I/O (or "overlapped" I/O in Win32 jargon) is used, we need to deal with the OVERLAPPED structure and his hEvent member. If the I/O function will delay the read or write operation, we will get an ERROR_IO_PENDING error code, then we will wait the asynchronous operation to complete with a WaitForXxxEvent function, then we will call GetOverlappedResult.

但是,如果 I/O 操作立即完成,我们将不会得到 ERROR_IO_PENDING,并且在读取操作中我们的读取缓冲区将立即被填满.但是 OVERLAPPED::hEvent 成员呢?它会被设置为信号状态吗?我没有找到明确的说法.

However, if the I/O operation is immediately completed, we will not get ERROR_IO_PENDING, and in a read operation our read buffer will be filled immediately. But what about the OVERLAPPED::hEvent member? Will it be set to signaled state? I've not found a clear statement about that.

这个问题似乎毫无意义(如果我知道操作已经完成,为什么还要处理事件?),但是我有一个模仿重叠模式的库,我需要有完全相同的行为.

This question may seem pointless (why deal with the event if I know that the operation is already completed?), however I have a library that mimics the overlapped pattern and I need to have the same exact behavior.

正如 edgar.holleis他的评论Raymond Chen 在他的博客中解释了这一点:http:///blogs.msdn.com/b/oldnewthing/archive/2014/02/06/10497096.aspx

As pointed by edgar.holleis in his comment, Raymond Chen explained this in his blog: http://blogs.msdn.com/b/oldnewthing/archive/2014/02/06/10497096.aspx

如果异步 I/O 同步完成,OVERLAPPED 结构中的 hEvent 是否仍然发出信号?

是的.

当一个 I/O 完成时(无论是同步还是异步),事件被发出信号并且完成状态通知被排队.这GetOverlappedResult/Ex 函数可用于等待 I/O已经完成;它只会立即返回.如果你问HasOverlappedIoCompleted I/O 是否完成,以及 I/O同步完成,它会正确报告,是的,当然是完全的.哎呀,它很久以前就完成了!"

When an I/O completes (whether synchronously or asynchronously), the event is signaled and completion status notifications are queued. The Get­Overlapped­Result/Ex function can be used to wait on an I/O that has already completed; it will merely return immediately. If you ask Has­Overlapped­Io­Completed whether the I/O has completed, and the I/O completed synchronously, it will correctly report, "Yeah, of course it completed. Heck, it completed a long time ago!"

换句话说,你可以从逻辑上处理异步的情况I/O 请求同步完成,就好像它已经完成一样异步.它只是在你之前异步完成眨眼.

In other words, you can logically treat the case of an asynchronous I/O request completing synchronously as if it had completed asynchronously. It just completes asynchronously before you even blinked.

推荐答案

不,不会.我花了很长时间才弄清楚这一点;)

No it won't. It took me ages to figure that one out the hard way ;)

这篇关于OVERLAPPED Win32 结构中的 hEvent 成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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