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) を呼び出すことによって作成されませんでした。

EndRead(IAsyncResult) は複数回呼び出されます。

ストリームが閉じているか、内部エラーが発生しました。

このコード例は、コンストラクターに対して提供されるより大きな例の 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は、 からBeginReadすべてIAsyncResultで呼び出すことができます。 を呼び出 EndRead すと、ストリームから読み取られたバイト数が示されます。 EndRead は、I/O 操作が完了するまでブロックされます。

適用対象

こちらもご覧ください