C# FileStream EndRead

In this chapter you will learn:

  1. Get to know FileStream.EndRead
  2. Syntax for FileStream.EndRead
  3. Parameter for FileStream.EndRead
  4. Returns for FileStream.EndRead
  5. Example - FileStream.EndRead

Description

FileStream EndRead Waits for the pending asynchronous read operation to complete. (Consider using ReadAsync instead; see the Remarks section.)

Syntax

FileStream.EndRead has the following syntax.


public override int EndRead(
  IAsyncResult asyncResult
)

Parameters

FileStream.EndRead has the following parameters.

  • asyncResult - The reference to the pending asynchronous request to wait for.

Returns

FileStream.EndRead method returns The number of bytes read from the stream, between 0 and the number of bytes you requested. Streams only return 0 at the end of the stream, otherwise, they should block until at least 1 byte is available.

Example


/*from w w  w  .ja  va 2s. c  o m*/
using System;
public class MainClass{
  public static void Main(String[] argv){  
     System.Console.WriteLine();
  }
  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;
         }
     }
     tempState.FStream.Close();
     tempState.ManualEvent.Set();
  }  
}
    

Next chapter...

What you will learn in the next chapter:

  1. Get to know FileStream.EndWrite
  2. Syntax for FileStream.EndWrite
  3. Parameter for FileStream.EndWrite
  4. Returns for FileStream.EndWrite
  5. Example - FileStream.EndWrite