FileStream.EndRead(IAsyncResult) 方法

定義

等候暫止的非同步讀取作業完成。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

要等候的暫止非同步要求的參考。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.

例外狀況

asyncResultnullasyncResult is null.

這個 IAsyncResult 物件不是透過對這個類別呼叫 BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) 所建立。This IAsyncResult object was not created by calling BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) on this class.

多次呼叫EndRead(IAsyncResult)EndRead(IAsyncResult) is called multiple times.

資料流已關閉或發生內部錯誤。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 中使用,以支援舊版程式碼;不過,新的非同步方法(例如 ReadAsyncWriteAsyncCopyToAsyncFlushAsync)可協助您更輕鬆地執行非同步檔案作業。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.

BeginRead的每個呼叫都必須完全呼叫 EndReadEndRead 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.

您可以從 BeginRead的每個 IAsyncResult 上呼叫 EndReadEndRead 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.

適用於

另請參閱