共用方式為


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

要等候的暫止非同步要求的參考。

傳回

自資料流讀取的位元組數,在 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();
}
let endReadCallback (asyncResult: IAsyncResult) =
    let tempState = asyncResult.AsyncState :?> State
    let readCount = tempState.FStream.EndRead asyncResult

    let mutable i = 0
    let mutable errored = false

    while i < readCount do
        if tempState.ReadArray[i] <> tempState.WriteArray[i] then
            printfn "Error writing data."
            tempState.FStream.Close()
            errored <- true
            i <- readCount

        i <- i + 1

    printfn $"The data was written to {tempState.FStream.Name} and verified."
    tempState.FStream.Close()
    // Signal the main thread that the verification is finished.
    tempState.ManualEvent.Set() |> ignore
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方法來實作異步檔案作業。 這些方法仍可在 .NET Framework 4.5 中使用,以支援舊版程式代碼;不過,新的異步方法,例如 ReadAsyncWriteAsyncCopyToAsyncFlushAsync,可協助您更輕鬆地實作異步檔案作業。

EndRead 每次呼叫 BeginRead都必須完全呼叫 。 在開始另一個讀取之前,無法結束讀取程式可能會導致不想要的行為,例如死結。

這個方法會覆寫 EndRead

EndRead可以從 每個上IAsyncResultBeginRead呼叫 。 呼叫 EndRead 會告訴您從數據流讀取多少個字節。 EndRead 將會封鎖,直到I/O作業完成為止。

適用於

另請參閱