다음을 통해 공유


FileStream.EndRead 메서드

보류 중인 비동기 읽기가 완료되기를 기다립니다.

네임스페이스: System.IO
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public Overrides Function EndRead ( _
    asyncResult As IAsyncResult _
) As Integer
‘사용 방법
Dim instance As FileStream
Dim asyncResult As IAsyncResult
Dim returnValue As Integer

returnValue = instance.EndRead(asyncResult)
public override int EndRead (
    IAsyncResult asyncResult
)
public:
virtual int EndRead (
    IAsyncResult^ asyncResult
) override
public int EndRead (
    IAsyncResult asyncResult
)
public override function EndRead (
    asyncResult : IAsyncResult
) : int

매개 변수

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

반환 값

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

예외

예외 형식 조건

ArgumentNullException

asyncResult가 Null 참조(Visual Basic의 경우 Nothing)인 경우

ArgumentException

이 클래스에 대해 BeginRead를 호출하여 이 IAsyncResult 개체가 만들어지지 않은 경우

InvalidOperationException

EndRead가 여러 번 호출되는 경우

설명

EndReadBeginRead를 호출할 때마다 한 번씩 호출해야 합니다. 다른 읽기 작업을 시작하기 전에 읽기 프로세스를 종료하지 못하면 교착 상태와 같이 원하지 않는 동작이 발생할 수 있습니다.

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

EndReadBeginRead의 각 IAsyncResult에 대해 호출될 수 있습니다. EndRead를 호출하면 스트림에서 읽은 바이트 수가 표시됩니다. EndRead는 I/O 작업이 완료될 때까지 차단됩니다.

다음 표에서는 일반적인 예 또는 관련된 I/O 작업의 예를 보여 줍니다.

수행 작업

참조 항목

텍스트 파일을 만듭니다.

방법: 파일에 텍스트 쓰기

텍스트 파일에 씁니다.

방법: 파일에 텍스트 쓰기

텍스트 파일에서 읽습니다.

방법: 파일의 텍스트 읽기

파일에 텍스트를 추가합니다.

방법: 로그 파일 열기 및 추가

File.AppendText

FileInfo.AppendText

파일 이름을 바꾸거나 이동합니다.

File.Move

FileInfo.MoveTo

파일을 복사합니다.

File.Copy

FileInfo.CopyTo

파일 크기를 가져옵니다.

FileInfo.Length

파일 특성을 가져옵니다.

File.GetAttributes

파일의 특성을 설정합니다.

File.SetAttributes

파일이 있는지 여부를 확인합니다.

File.Exists

이진 파일에서 읽습니다.

방법: 새로 만든 데이터 파일 읽기 및 쓰기

이진 파일에 씁니다.

방법: 새로 만든 데이터 파일 읽기 및 쓰기

디렉터리를 만듭니다.

Directory.CreateDirectory

Directory.CreateDirectory

예제

이 코드 예제는 FileStream(String,FileMode,FileAccess,FileShare,Int32,Boolean) 생성자에 대해 제공되는 보다 큰 예제의 일부입니다.

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
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();
}
   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.get_AsyncState()));
    int readCount = tempState.get_FStream().EndRead(asyncResult);
    int i = 0;
    while((i < readCount)) {
        if ( tempState.get_ReadArray()[i] != 
                tempState.get_WriteArray()[i++] ) {
            Console.WriteLine("Error writing data.");
            tempState.get_FStream().Close();
            return;
        }
    }
    Console.WriteLine("The data was written to {0} and verified.",
        tempState.get_FStream().get_Name());
    tempState.get_FStream().Close();

    // Signal the main thread that the verification is finished.
    tempState.get_ManualEvent().Set();
} //EndReadCallback

플랫폼

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

FileStream 클래스
FileStream 멤버
System.IO 네임스페이스

기타 리소스

파일 및 스트림 I/O
방법: 파일의 텍스트 읽기
방법: 파일에 텍스트 쓰기
비동기 파일 I/O