简体   繁体   English

测试readbytes是否等于在异步模式下使用ReadFile读取字节

[英]test if readbytes is equal to toread bytes using ReadFile in asynchronous mode

I have an error scenario wherein im trying to read more bytes than actually present in the file. 我有一个错误情况,其中我试图读取的字节数比文件中实际存在的字节数多。 In synchronous case it fails and appropriate error is reported because we call waitFrObject and wait till the io is complete and test if readbytes >= ToReadbytes. 在同步情况下,它失败并报告适当的错误,因为我们调用了waitFrObject并等待直到io完成并测试readbytes> = ToReadbytes。 But in case of asynchronous we start reading , ignore IO peding status flage returned by ReadFile( which is expected) and continue our operation. 但是在异步的情况下,我们开始读取,忽略ReadFile返回的IO peding状态标志(这是预期的)并继续我们的操作。 So the error is actually not caught in asynchronous case. 因此,在异步情况下实际上并未捕获该错误。 Now if i check wait for the IO to complete to check if readbytes>=ToreadBytes then it wont be asynchronous anymore as we are waitng. 现在,如果我检查等待IO完成以检查readbytes> = ToreadBytes,那么它将不再是异步的,因为我们在等待。 So how do i deal with such a situation? 那么我该如何处理这种情况呢? ( Note -Reading more bytes than actually present is one of the ways to make error reproducible) (注意-读取比实际多的字节是使错误可重现的方法之一)

Im using windown's ReadFile function for reading. 我正在使用windown的ReadFile函数进行读取。

Presumably you have some other thread that is waiting for the event triggered by the read completion, which will then go on to use the data. 大概您有其他线程正在等待读取完成触发的事件,然后该线程将继续使用数据。 Just add the check after the wait, before you use the data. 在使用数据之前,只需在等待后添加检查即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM