FileStream.EndRead(IAsyncResult) 方法

定义

等待挂起的异步读操作完成。 (请考虑改用 ReadAsync(Byte[], Int32, Int32, CancellationToken)。)

public:
 override int EndRead(IAsyncResult ^ asyncResult);
public override int EndRead (IAsyncResult asyncResult);
override this.EndRead : IAsyncResult -> int
Public Overrides Function EndRead (asyncResult As IAsyncResult) As Integer

参数

asyncResult
IAsyncResult

对所等待的挂起异步请求的引用。

返回

Int32

从流中读取的字节数,介于 0 和所请求的字节数之间。 流仅在流结尾返回 0,否则在至少有 1 个字节可用之前应一直进行阻止。

例外

asyncResultnull

IAsyncResult 对象不是通过对该类调用 BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) 来创建的。

流已关闭或出现了内部错误。

示例

此代码示例是为构造函数提供的较大示例的 FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) 一部分。

   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.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();
}
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

注解

在 .NET Framework 4 及更早版本中,你必须使用 和 等方法来 BeginRead EndRead 实现异步文件操作。 这些方法仍可用于 .NET Framework 4.5 以支持旧代码;但是,新的异步方法(如 、、 和 )可帮助你更轻松地实现异步 ReadAsync WriteAsync CopyToAsync FlushAsync 文件操作。

EndRead 必须完全针对每次对 的调用调用 BeginRead 。 在开始另一个读取之前未能结束读取进程可能会导致意外行为,例如死锁。

此方法重写 EndRead

EndRead 可以从 中对每个 IAsyncResult 调用 BeginRead 。 调用 EndRead 可指示从流中读取的字节数。 EndRead 将阻止 ,直到 I/O 操作完成。

适用于

另请参阅