Begins an asynchronous read operation. (Consider using FileStream.ReadAsync(Byte[], int, int, System.Threading.CancellationToken) instead; see the Remarks section.)
- 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.
An object that references the asynchronous read.
Type Reason ArgumentException The sum of offset andnumBytes is greater than the length of array. ArgumentNullException array is null. ArgumentOutOfRangeException offset or numBytes is negative. System.IO.IOException The asynchronous read operation attempted to read past the end of the file.
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.
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.