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.

例外

asyncResultnull です。asyncResult 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.

ストリームが閉じているか、内部エラーが発生しました。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のメソッドは、レガシコードをサポートするためにでも使用できます。ただし、、、 WriteAsync CopyToAsyncFlushAsyncなどの新しい非同期メソッドReadAsyncは、非同期のファイル操作をより簡単に実装するのに役立ちます。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を呼び出すたびに、をBeginRead正確に呼び出す必要があります。EndRead 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.

このメソッドは、EndRead をオーバーライドします。This method overrides EndRead.

EndReadは、のすべてIAsyncResult BeginReadので呼び出すことができます。EndRead 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.

適用対象

こちらもご覧ください