首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对ReadFile的调用返回ERROR_WORKING_SET_QUOTA

对ReadFile的调用返回ERROR_WORKING_SET_QUOTA
EN

Stack Overflow用户
提问于 2010-08-10 01:06:41
回答 1查看 1.5K关注 0票数 0

程序正常运行几分钟后,ReadFile开始失败,错误代码为ERROR_WORKING_SET_QUOTA。

我使用带有重叠I/O的ReadFile,如下所示:

代码语言:javascript
复制
while (continueReading)
{
    BOOL bSuccess = ReadFile(deviceHandle, pReadBuf, length, 
                             &bytesRead, readOverlappedPtr);
    waitVal = WaitForMultipleObjects(
                (sizeof(eventsToWaitFor)/sizeof(eventsToWaitFor[0])), 
                eventsToWaitFor, FALSE, INFINITE);
    if (waitVal == WAIT_OBJECT_0) {
       // do stuff
    } else if (waitVal == WAIT_OBJECT_0 + 1) {
       // do stuff
    } else if (waitVal == WAIT_OBJECT_0 + 2) {
       // complete the read
       bSuccess = GetOverlappedResult(deviceHandle, &readOverlapped, 
                                      &bytesRead, FALSE);
       if (!bSuccess) {
          errorCode = GetLastError();
          printf("ReadFile error=%d\n", errorCode);
       }
    }
}

为什么我会得到这个错误?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-08-10 01:12:44

问题是调用ReadFile的次数比调用GetOverlappedResult的次数多。导致进程耗尽用于处理所有未完成读取的资源。

此外,我们应该检查ReadFile的结果,并确保结果为ERROR_IO_PENDING,如果不是并且ReadFile返回FALSE,那么就有另一个问题。

确保每次成功调用ReadFile时都调用一次GetOverlappedResult。如下所示:

代码语言:javascript
复制
BOOL bPerformRead = TRUE;
while (continueReading) 
{ 
    BOOL bSuccess = TRUE;
    // only perform the read if the last one has finished
    if (bPerformRead) {
       bSuccess = ReadFile(deviceHandle, pReadBuf, length,
                           &bytesRead, readOverlappedPtr);
       if (!bSuccess) {
          errorCode = GetLastError();
          if (errorCode != ERROR_IO_PENDING) {
             printf("ReadFile error=%d\n", errorCode); 
             return;
          }
       } else {
          // read completed right away
          continue;
       }
       // we can't perform another read until this one finishes
       bPerformRead = FALSE;
    }
    waitVal = WaitForMultipleObjects( 
                (sizeof(eventsToWaitFor)/sizeof(eventsToWaitFor[0])),  
                eventsToWaitFor, FALSE, INFINITE); 
    if (waitVal == WAIT_OBJECT_0) { 
       // do stuff 
    } else if (waitVal == WAIT_OBJECT_0 + 1) { 
       // do stuff 
    } else if (waitVal == WAIT_OBJECT_0 + 2) { 
       // complete the read 
       bSuccess = GetOverlappedResult(deviceHandle, &readOverlapped,  
                                      &bytesRead, FALSE); 
       // the read is finished, we can read again
       bPerformRead = TRUE;
       if (!bSuccess) { 
          errorCode = GetLastError(); 
          printf("GetOverlappedResult from ReadFile error=%d\n", errorCode); 
       } 
    } 
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3442385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档