System.IO.FileStream.BeginRead Method

Begins an asynchronous read operation. (Consider using FileStream.ReadAsync(Byte[], int, int, System.Threading.CancellationToken) instead; see the Remarks section.)

Syntax

public override IAsyncResult BeginRead (byte[] array, int offset, int numBytes, AsyncCallback userCallback, object stateObject)

Parameters

array
The buffer to read data into.
offset
The byte offset in array at which to begin reading.
numBytes
The maximum number of bytes to read.
userCallback
The method to be called when the asynchronous read operation is completed.
stateObject
A user-provided object that distinguishes this particular asynchronous read request from other requests.

Returns

An object that references the asynchronous read.

Exceptions

TypeReason
ArgumentExceptionThe sum of offset andnumBytes is greater than the length of array.
ArgumentNullException array is null.
ArgumentOutOfRangeException offset or numBytes is negative.
System.IO.IOExceptionThe asynchronous read operation attempted to read past the end of the file.

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 once 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.

System.IO.FileStream provides two different modes of operation: synchronous I/O and asynchronous I/O. While either can be used, the underlying operating system resources might allow access in only one of these modes. By default, System.IO.FileStream opens the operating system handle synchronously. In Windows, this slows down asynchronous methods. If asynchronous methods are used, use the FileStream.#ctor(string, FileMode, FileAccess, FileShare, int, bool) constructor.

Note:

Use the FileStream.CanRead property to determine whether the current instance supports reading. For additional information, see Stream.CanRead.

If a stream is closed or you pass an invalid argument, exceptions are thrown immediately from FileStream.BeginRead(Byte[], int, int, AsyncCallback, object). Errors that occur during an asynchronous read request, such as a disk failure during the IO request, occur on the thread pool thread and become visible upon a call to FileStream.EndRead(IAsyncResult).

Stream.EndRead(IAsyncResult) must be called with this IAsyncResult to find out how many bytes were read.

Multiple simultaneous asynchronous requests render the request completion order uncertain.

For a list of common file and directory operations, see Common I/O Tasks.

Requirements

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