FileStream.EndRead 方法
等待挂起的异步读取完成。
**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Overrides Function EndRead ( _
asyncResult As IAsyncResult _
) As Integer
用法
Dim instance As FileStream
Dim asyncResult As IAsyncResult
Dim returnValue As Integer
returnValue = instance.EndRead(asyncResult)
public override int EndRead (
IAsyncResult asyncResult
)
public:
virtual int EndRead (
IAsyncResult^ asyncResult
) override
public int EndRead (
IAsyncResult asyncResult
)
public override function EndRead (
asyncResult : IAsyncResult
) : int
参数
- asyncResult
对所等待的挂起异步请求的引用。
返回值
从流中读取的字节数,介于 0 和所请求的字节数之间。流仅在流的末尾返回 0;否则应一直阻塞到至少有 1 个字节可用为止。
异常
异常类型 | 条件 |
---|---|
asyncResult 为 空引用(在 Visual Basic 中为 Nothing)。 |
|
此 IAsyncResult 对象不是通过对该类调用 BeginRead 来创建的。 |
|
EndRead 被多次调用。 |
备注
每次调用 BeginRead 时都必须调用 EndRead。无法结束一个读取进程就开始另一个读取可能导致死锁等不希望出现的行为。
此方法重写 EndRead。
可针对 BeginRead 的每个 IAsyncResult 调用 EndRead。对 EndRead 的调用将告诉您从流中读取的字节数。EndRead 将一直阻止到完成 I/O 操作为止。
下表列出了其他典型或相关的 I/O 任务的示例。
若要执行此操作... |
请参见本主题中的示例... |
---|---|
创建文本文件。 |
|
写入文本文件。 |
|
读取文本文件。 |
|
向文件中追加文本。 |
|
重命名或移动文件。 |
|
复制文件。 |
|
获取文件大小。 |
|
获取文件属性。 |
|
设置文件属性。 |
|
确定文件是否存在。 |
|
读取二进制文件。 |
|
写入二进制文件。 |
|
创建目录。 |
Directory.CreateDirectory |
示例
此代码示例是为 FileStream(String,FileMode,FileAccess,FileShare,Int32,Boolean) 构造函数提供的一个更大示例的一部分。
Private Shared Sub EndReadCallback(asyncResult As IAsyncResult)
Dim tempState As State = _
DirectCast(asyncResult.AsyncState, State)
Dim readCount As Integer = _
tempState.FStream.EndRead(asyncResult)
Dim i As Integer = 0
While(i < readCount)
If(tempState.ReadArray(i) <> tempState.WriteArray(i))
Console.WriteLine("Error writing data.")
tempState.FStream.Close()
Return
End If
i += 1
End While
Console.WriteLine("The data was written to {0} and " & _
"verified.", tempState.FStream.Name)
tempState.FStream.Close()
' Signal the main thread that the verification is finished.
tempState.ManualEvent.Set()
End Sub
static void EndReadCallback(IAsyncResult asyncResult)
{
State tempState = (State)asyncResult.AsyncState;
int readCount = tempState.FStream.EndRead(asyncResult);
int i = 0;
while(i < readCount)
{
if(tempState.ReadArray[i] != tempState.WriteArray[i++])
{
Console.WriteLine("Error writing data.");
tempState.FStream.Close();
return;
}
}
Console.WriteLine("The data was written to {0} and verified.",
tempState.FStream.Name);
tempState.FStream.Close();
// Signal the main thread that the verification is finished.
tempState.ManualEvent.Set();
}
static void EndReadCallback( IAsyncResult^ asyncResult )
{
State^ tempState = dynamic_cast<State^>(asyncResult->AsyncState);
int readCount = tempState->FStream->EndRead( asyncResult );
int i = 0;
while ( i < readCount )
{
if ( tempState->ReadArray[ i ] != tempState->WriteArray[ i++ ] )
{
Console::WriteLine( "Error writing data." );
tempState->FStream->Close();
return;
}
}
Console::WriteLine( "The data was written to {0} "
"and verified.", tempState->FStream->Name );
tempState->FStream->Close();
// Signal the main thread that the verification is finished.
tempState->ManualEvent->Set();
}
public:
static void EndReadCallback(IAsyncResult asyncResult)
{
State tempState = ((State)(asyncResult.get_AsyncState()));
int readCount = tempState.get_FStream().EndRead(asyncResult);
int i = 0;
while((i < readCount)) {
if ( tempState.get_ReadArray()[i] !=
tempState.get_WriteArray()[i++] ) {
Console.WriteLine("Error writing data.");
tempState.get_FStream().Close();
return;
}
}
Console.WriteLine("The data was written to {0} and verified.",
tempState.get_FStream().get_Name());
tempState.get_FStream().Close();
// Signal the main thread that the verification is finished.
tempState.get_ManualEvent().Set();
} //EndReadCallback
平台
Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
FileStream 类
FileStream 成员
System.IO 命名空间