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 = (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は、IAsyncResultからすべてのBeginReadで呼び出すことができます。 EndReadを呼び出すと、ストリームから読み取られたバイト数が示されます。 EndRead は、I/O 操作が完了するまでブロックします。

適用対象

こちらもご覧ください