System.IO.FileStream.EndRead Method

Waits for the pending asynchronous read operation to complete. (Consider using FileStream.ReadAsync(Byte[], int, int, System.Threading.CancellationToken) instead; see the Remarks section.)

Syntax

public override int EndRead (IAsyncResult asyncResult)

Parameters

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

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.

Exceptions

TypeReason
ArgumentNullException asyncResult is null.
ArgumentException asyncResult was not returned by a call to FileStream.BeginRead(Byte[], int, int, AsyncCallback, object).
InvalidOperationException FileStream.EndRead(IAsyncResult) was called multiple times with asyncResult .

Remarks

In the .NET Framework 4 and earlier versions, you have to use methods such as FileStream.BeginRead(Byte[], int, int, AsyncCallback, object) and FileStream.EndRead(IAsyncResult) to implement asynchronous file operations. These methods are still available in the net_v45 to support legacy code; however, the new async methods, such as FileStream.ReadAsync(Byte[], int, int, System.Threading.CancellationToken), FileStream.WriteAsync(Byte[], int, int, System.Threading.CancellationToken), Stream.CopyToAsync(Stream), and FileStream.FlushAsync(System.Threading.CancellationToken), help you implement asynchronous file operations more easily.

FileStream.EndRead(IAsyncResult) must be called exactly for every call to FileStream.BeginRead(Byte[], int, int, AsyncCallback, object). Failing to end a read process before beginning another read can cause undesirable behavior such as deadlock.

This method overrides Stream.EndRead(IAsyncResult).

FileStream.EndRead(IAsyncResult) can be called on every IAsyncResult from FileStream.BeginRead(Byte[], int, int, AsyncCallback, object). Calling FileStream.EndRead(IAsyncResult) tells you how many bytes were read from the stream. FileStream.EndRead(IAsyncResult) will block until the I/O operation has completed.

Requirements

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0