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입니다.

이 클래스를 호출 BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) 하여 이 IAsyncResult 개체를 만들지 않았습니다.

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

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

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

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

적용 대상

추가 정보