BeginRead Método (Byte[], Int32, Int32, AsyncCallback, Object)
Este artículo proviene de un motor de traducción automática. Mueva el puntero sobre las frases del artículo para ver el texto original. Más información.

FileStream.BeginRead (Método) (Byte[], Int32, Int32, AsyncCallback, Object)


Begins an asynchronous read operation. (Consider using ReadAsync instead; see the Remarks section.)

Espacio de nombres:   System.IO
Ensamblado:  mscorlib (en mscorlib.dll)

[HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading = true)]
public override IAsyncResult BeginRead(
	byte[] array,
	int offset,
	int numBytes,
	AsyncCallback userCallback,
	object stateObject


Type: System.Byte[]

The buffer to read data into.

Type: System.Int32

The byte offset in array at which to begin reading.

Type: System.Int32

The maximum number of bytes to read.

Type: System.AsyncCallback

The method to be called when the asynchronous read operation is completed.

Type: System.Object

A user-provided object that distinguishes this particular asynchronous read request from other requests.

Valor devuelto

Type: System.IAsyncResult

An object that references the asynchronous read.

Exception Condition

The array length minus offset is less than numBytes.


array is null.


offset or numBytes is negative.


An asynchronous read was attempted past the end of the file.

In the .NET Framework 4 and earlier versions, you have to use methods such as M:System.IO.FileStream.BeginRead(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object) and M:System.IO.FileStream.EndRead(System.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 M:System.IO.FileStream.ReadAsync(System.Byte[],System.Int32,System.Int32,System.Threading.CancellationToken), M:System.IO.FileStream.WriteAsync(System.Byte[],System.Int32,System.Int32,System.Threading.CancellationToken), M:System.IO.Stream.CopyToAsync(System.IO.Stream), and M:System.IO.FileStream.FlushAsync(System.Threading.CancellationToken), help you implement asynchronous file operations more easily.

EndRead must be called exactly once for every call to BeginRead. Failing to end a read process before beginning another read can cause undesirable behavior such as deadlock.

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, FileStream opens the operating system handle synchronously. In Windows, this slows down asynchronous methods. If asynchronous methods are used, use the FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) constructor.


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

If a stream is closed or you pass an invalid argument, exceptions are thrown immediately from M:System.IO.FileStream.BeginRead(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.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 M:System.IO.FileStream.EndRead(System.IAsyncResult).

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

This code example is part of a larger example provided for the M:System.IO.FileStream.#ctor(System.String,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,System.Int32,System.Boolean) constructor.

static void EndWriteCallback(IAsyncResult asyncResult)
    State tempState = (State)asyncResult.AsyncState;
    FileStream fStream = tempState.FStream;

    // Asynchronously read back the written data.
    fStream.Position = 0;
    asyncResult = fStream.BeginRead(
        tempState.ReadArray, 0 , tempState.ReadArray.Length, 
        new AsyncCallback(EndReadCallback), tempState);

    // Concurrently do other work, such as 
    // logging the write operation.

.NET Framework
Disponible desde 1.1
Disponible desde 2.0
Windows Phone Silverlight
Disponible desde 7.0
Volver al principio
© 2016 Microsoft