0

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

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

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);
       }
    }
}

为什么我会收到此错误?

4

1 回答 1

1

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

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

确保GetOverlappedResult每次成功调用ReadFile. 像这样:

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); 
       } 
    } 
}
于 2010-08-09T17:12:44.480 回答