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

대기할 보류 중인 비동기 요청에 대한 참조입니다.

반환

Int32

0과 요청한 바이트 수 사이의 스트림에서 읽은 바이트 수입니다. 스트림은 스트림의 끝에서 0만을 반환하며 그렇지 않으면 최소한 1바이트를 사용할 수 있을 때까지 차단됩니다.

예외

asyncResult이(가) null인 경우

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();
}
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 및 이전 버전에서는 비동기 파일 작업과 EndRead 같은 BeginRead 메서드를 사용하고 구현해야 합니다. 이러한 메서드는 레거시 코드를 지원하기 위해 .NET Framework 4.5에서 계속 사용할 수 있지만, 새로운 비동기 메서드(예: ReadAsync, WriteAsyncCopyToAsyncFlushAsync)는 비동기 파일 작업을 보다 쉽게 구현하는 데 도움이 됩니다.

EndRead 에 대한 모든 호출에 대해 정확히 호출되어야 합니다 BeginRead. 다른 읽기를 시작하기 전에 읽기 프로세스를 종료하지 못하면 교착 상태와 같은 바람직하지 않은 동작이 발생할 수 있습니다.

이 메서드는 EndRead를 재정의합니다.

EndRead 에서 호출 IAsyncResult BeginRead할 수 있습니다. 호출 EndRead 은 스트림에서 읽은 바이트 수를 알려줍니다. EndRead 는 I/O 작업이 완료될 때까지 차단됩니다.

적용 대상

추가 정보