FileStream.EndRead Method

Waits for the pending asynchronous read to complete.

Namespace: System.IO
Assembly: mscorlib (in 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 int EndRead (
	IAsyncResult asyncResult
public override function EndRead (
	asyncResult : IAsyncResult
) : int
Not applicable.



The reference to the pending asynchronous request to wait for.

Return Value

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.

Exception typeCondition


asyncResult is a null reference (Nothing in Visual Basic).


This IAsyncResult object was not created by calling BeginRead on this class.


EndRead is called multiple times.


The stream is closed or an internal error has occurred.

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.

This method overrides EndRead.

EndRead can be called on every IAsyncResult from BeginRead. Calling EndRead tells you how many bytes were read from the stream. EndRead will block until the I/O operation has completed.

For a list of common I/O tasks, see Common I/O Tasks.

Windows Mobile 2003 for Pocket PC, Windows Mobile 2003 for Smartphone, Windows CE Platform Note: In .NET Compact Framework applications, this method is supported, but it is reserved for future use. Using this method will raise a NotSupportedException until it becomes available in a future release.

This code example is part of a larger example provided for the FileStream(String,FileMode,FileAccess,FileShare,Int32,Boolean) constructor.

Private Shared Sub EndReadCallback(asyncResult As IAsyncResult)
     Dim tempState As State = _
         DirectCast(asyncResult.AsyncState, State)
     Dim readCount As Integer = _

     Dim i As Integer = 0
     While(i < readCount)
         If(tempState.ReadArray(i) <> tempState.WriteArray(i))
             Console.WriteLine("Error writing data.")
         End If
         i += 1
     End While

     Console.WriteLine("The data was written to {0} and " & _
         "verified.", tempState.FStream.Name)

     ' Signal the main thread that the verification is finished.
 End Sub

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.");
    Console.WriteLine("The data was written to {0} and verified.",

    // Signal the main thread that the verification is finished.
} //EndReadCallback

Windows 98, Windows Server 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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions