FileStream.EndRead(IAsyncResult) FileStream.EndRead(IAsyncResult) FileStream.EndRead(IAsyncResult) FileStream.EndRead(IAsyncResult) Method

定义

等待挂起的异步读操作完成。Waits for the pending asynchronous read operation to complete. (请考虑改用 ReadAsync(Byte[], Int32, Int32, CancellationToken)。)(Consider using ReadAsync(Byte[], Int32, Int32, CancellationToken) instead.)

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 IAsyncResult IAsyncResult IAsyncResult

对所等待的挂起异步请求的引用。The reference to the pending asynchronous request to wait for.

返回

从流中读取的字节数,介于 0 和所请求的字节数之间。The number of bytes read from the stream, between 0 and the number of bytes you requested. 流仅在流结尾返回 0,否则在至少有 1 个字节可用之前应一直进行阻止。Streams only return 0 at the end of the stream, otherwise, they should block until at least 1 byte is available.

异常

流已关闭或出现了内部错误。The stream is closed or an internal error has occurred.

示例

此代码示例是为提供一个更大示例的一部分FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean)构造函数。This code example is part of a larger example provided for the FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) constructor.

   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 和更早版本中,您必须使用如下所示方法BeginReadEndRead实现异步文件操作。In the .NET Framework 4 and earlier versions, you have to use methods such as BeginRead and EndRead to implement asynchronous file operations. 这些方法是在中仍然可用.NET Framework 4.5.NET Framework 4.5以支持旧版代码; 但是,新的异步方法,如ReadAsyncWriteAsyncCopyToAsync,和FlushAsync,帮助您更轻松地实现异步文件操作。These methods are still available in the .NET Framework 4.5.NET Framework 4.5 to support legacy code; however, the new async methods, such as ReadAsync, WriteAsync, CopyToAsync, and FlushAsync, help you implement asynchronous file operations more easily.

EndRead 必须正好为每个调用调用BeginReadEndRead must be called exactly for every call to BeginRead. 无法开始另一个读取之前结束读取的进程可能会导致意外的行为,例如死锁。Failing to end a read process before beginning another read can cause undesirable behavior such as deadlock.

此方法重写 EndReadThis method overrides EndRead.

EndRead 可以调用每个IAsyncResultBeginReadEndRead can be called on every IAsyncResult from BeginRead. 调用EndRead告诉您从流读取字节数。Calling EndRead tells you how many bytes were read from the stream. EndRead 之前已完成 I/O 操作将阻塞。EndRead will block until the I/O operation has completed.

适用于

另请参阅