我正在尝试使用 WaitForMultipleObjects() 来等待多个异步 ReadFile() 完成,但是它总是立即返回并且没有读取任何字节。另一方面,GetOverlappedResult() 总是成功的。我不确定我做错了什么。
我在下面的这段代码中只使用一个异步 ReadFile() 来测试它。
events = (HANDLE *)malloc(sizeof(HANDLE));
buffer = (BYTE*)malloc(BufferSize*sizeof(BYTE));
memset(buffer, 0, BufferSize*sizeof(BYTE));
hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!hEvent){
printf("can't create event\n");
return 0;
}
memset(&testOv, 0, sizeof(testOv));
testOv.hEvent = hEvent; //event to use with waitformultipleobject()
testFile = CreateFile(SrcDst[0][0], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (INVALID_HANDLE_VALUE==testFile)
{
printf("Unable to open file %s. Error=%d\n",SrcDst[0][0], GetLastError());
return 0;
}
events[0] = testOv.hEvent;
RF = ReadFile(testFile, buffer, 25, &byteRead, &testOv);
//WaitForMultipleObjects(1, events, TRUE, INFINITE); return before any byte is read
//WaitForSingleObject(testFile, INFINITE); waiting indefinitely
GetOverlappedResult(testFile, &testOv, &byteRead, TRUE);
for (i = 0; i < byteRead; i++){
printf("%c", (char)buffer[i]);
}
printf("byteRead: %d\n", byteRead);
printf("error: %d\n", GetLastError());