ReadFileEx方法读文件,为何回调函数没被触发?解决办法
ReadFileEx方法读文件,为何回调函数没被触发?
不知道为什么FileIOCompletionRoutine函数没进去,代码如下:
请教高手,不知道哪里写的不对?还是我对ReadFileEx用法理解有误?
------解决方案--------------------
Sleep(1000);
-->
SleepEx(1000, TRUE);
不知道为什么FileIOCompletionRoutine函数没进去,代码如下:
- C/C++ code
#include<iostream> #include<string> #include<Windows.h> using namespace std; VOID CALLBACK FileIOCompletionRoutine(DWORD dwErrorCode,DWORD dwNumberOfBytesTransfered,LPOVERLAPPED lpOverlapped) { cout<<"enter call back"<<endl; } int main(void) { HANDLE hEvent=NULL; HANDLE hFile=NULL; const int buffSize=512; char *dataBuffer=new char[buffSize]; hFile = CreateFile( TEXT("a.txt"), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL ); if(hFile==INVALID_HANDLE_VALUE) return 0; hEvent=CreateEvent(NULL,true,false,NULL); if(hEvent==NULL) return 0; OVERLAPPED ovlap; ZeroMemory(&ovlap,sizeof(ovlap)); ovlap.hEvent=hEvent; bool fNotEnd=ReadFileEx ( hFile, dataBuffer, buffSize, &ovlap, (LPOVERLAPPED_COMPLETION_ROUTINE) FileIOCompletionRoutine ); while(true) { cout<<"loop"<<endl; Sleep(1000); } return 0; }
请教高手,不知道哪里写的不对?还是我对ReadFileEx用法理解有误?
------解决方案--------------------
Sleep(1000);
-->
SleepEx(1000, TRUE);