WaitForMultipleObjects或WaitForSingleObject可以与异步ReadFile()一起使用吗?

时间:2013-02-10 22:48:34

标签: c asynchronous io

我正在尝试使用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());

0 个答案:

没有答案