各位老大,在vc中用BOOL ReadFile(
  HANDLE hFile,                // handle of file to read
  LPVOID lpBuffer,             // pointer to buffer that receives data
  DWORD nNumberOfBytesToRead,  // number of bytes to read
  LPDWORD lpNumberOfBytesRead, // pointer to number of bytes read
  LPOVERLAPPED lpOverlapped    // pointer to structure for data
);时,他的第五个参数可以设置读文件为异步方式,但必须用getoverlappedresult()函数来等待这个读函数完成,
但是,第五个参数不设置为异步方式,在读数据时也要等待,读数据完成,那么这两种等待方式有什么不同,请各位楼主帮忙?
小弟提前谢了

解决方案 »

  1.   

    msdn的例子
    Example Code [C++]
    The following code example shows you how to test for end-of-file for an asynchronous read operation.
    OVERLAPPED gOverlapped;// Set up overlapped structure fields.
    gOverlapped.Offset     = 0; 
    gOverlapped.OffsetHigh = 0; 
    gOverlapped.hEvent     = hEvent; // Verify that sizeof(inBuffer <= nBytestoRead).
     
    // Attempt an asynchronous read operation.
    bResult = ReadFile(hFile, &inBuffer, nBytesToRead, &nBytesRead, 
        &gOverlapped) ; 
     
    // If there was a problem, or if the asynchronous operation 
    // is still pending. 
    if (!bResult) 

        // Deal with the error code. 
        switch (dwError = GetLastError()) 
        { 
            case ERROR_HANDLE_EOF: 
            { 
                // WCode to handle the end of the file 
                // during the call to ReadFile 
     
                 
            } 
     
            case ERROR_IO_PENDING: 
            { 
                // Asynchronous I/O is still in progress
     
                // Do something else for a while 
                GoDoSomethingElse() ; 
     
                // Check on the results of the asynchronous read. 
                bResult = GetOverlappedResult(hFile, &gOverlapped, 
                    &nBytesRead, FALSE) ; 
     
                // If there was a problem  
                if (!bResult) 
                { 
                    // Deal with the error code. 
                    switch (dwError = GetLastError()) 
                    { 
                        case ERROR_HANDLE_EOF: 
                        { 
                            // We have reached the end of
                            // the file during asynchronous
                            // operation.
                        } 
     
                        // Deal with other error cases. 
                    }   //end switch (dwError = GetLastError()) 
                 } 
            } // end case 
     
            // Deal with other error cases, such as the default. 
     
        } // end switch (dwError = GetLastError()) 
     } // end if
      

  2.   

    同步模式下进行IO你没有选择,只能等IO结束。(控制权交给了内核)
    异步模式下进行IO可以等,也可以不等,等的不爽了SetEvent就结束了他。(内核给我们一个机会来参与控制IO)