各位老大,在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()函数来等待这个读函数完成,
但是,第五个参数不设置为异步方式,在读数据时也要等待,读数据完成,那么这两种等待方式有什么不同,请各位楼主帮忙?
小弟提前谢了
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()函数来等待这个读函数完成,
但是,第五个参数不设置为异步方式,在读数据时也要等待,读数据完成,那么这两种等待方式有什么不同,请各位楼主帮忙?
小弟提前谢了
解决方案 »
- libcurl重用TCP连接的问题
- 急!用MSDN里的CreateProcess却启动不了另一EXE程序,不知道哪里发生问题了.
- 怎么老是出现“IDispatch error #3092”错误,怀疑是记录集创建出错,请高手指教!
- 对话框程序,改变了背景颜色再改变了EDIT控件颜色,但EDIT控件的白色边(3D边框)不知如何改颜色?
- 急:如何在MDI子窗体间发送消息?如何在MDI子窗体间发送消息?如何在MDI子窗体间发送消息?
- 如何在MyDoc类循环处理的数据每次能够在MyView类OnDraw里显示?
- 多个Socket中,如何知道是哪个Socket与消息发送方传递消息???
- 请大家帮帮忙!:::::)
- 我的《信 誉 值》 为什么会减了的??
- 需要FAT32的详细资料。(已经是我能给的最高分了,呵呵)
- 重载+号时,编译器告诉我参数太多??
- 如何得到IE访问错误的消息,比如页面上显示HTTP 500的错误!
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
异步模式下进行IO可以等,也可以不等,等的不爽了SetEvent就结束了他。(内核给我们一个机会来参与控制IO)