Export (0) Print
Expand All

FileStream.BeginRead Method

Begins an asynchronous read.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

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

Parameters

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

Return Value

Type: System.IAsyncResult
An object that references the asynchronous read.

ExceptionCondition
ArgumentException

The array length minus offset is less than numBytes.

ArgumentNullException

array is null.

ArgumentOutOfRangeException

offset or numBytes is negative.

IOException

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

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.

NoteNote

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

If a stream is closed or you pass an invalid argument, exceptions are thrown immediately from BeginRead. 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 EndRead.

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 I/O tasks, see Common I/O Tasks.

NoteNote

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

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


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

    // 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

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft