This documentation is archived and is not being maintained.

FileStream.BeginRead Method

Begins an asynchronous read.

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

'Declaration
Public Overrides Function BeginRead ( _
	array As Byte(), _
	offset As Integer, _
	numBytes As Integer, _
	userCallback As AsyncCallback, _
	stateObject As Object _
) As IAsyncResult
'Usage
Dim instance As FileStream
Dim array As Byte()
Dim offset As Integer
Dim numBytes As Integer
Dim userCallback As AsyncCallback
Dim stateObject As Object
Dim returnValue As IAsyncResult

returnValue = instance.BeginRead(array, offset, numBytes, userCallback, stateObject)
public IAsyncResult BeginRead (
	byte[] array, 
	int offset, 
	int numBytes, 
	AsyncCallback userCallback, 
	Object stateObject
)
public override function BeginRead (
	array : byte[], 
	offset : int, 
	numBytes : int, 
	userCallback : AsyncCallback, 
	stateObject : Object
) : IAsyncResult
Not applicable.

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.

Return Value

An IAsyncResult that references the asynchronous read.

Exception typeCondition

ArgumentException

The array length minus offset is less than numBytes.

ArgumentNullException

array is a null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

offset or numBytes is negative.

IOException

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

EndRead must be called exactly 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.

NoteNote:

On Windows, all I/O operations smaller than 64 KB will complete synchronously for better performance. Asynchronous I/O might hinder performance for buffer sizes smaller than 64 KB.

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.

Windows Mobile 2003 for Pocket PC, Windows Mobile 2003 for Smartphone, Windows CE Platform Note: In .NET Compact Framework applications, this method is supported, but it is reserved for future use. Using this method will raise a NotSupportedException until it becomes available in a future release.

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

Private Shared Sub EndWriteCallback(asyncResult As IAsyncResult)
    Dim tempState As State = _
        DirectCast(asyncResult.AsyncState, State)
    Dim fStream As FileStream = tempState.FStream
    fStream.EndWrite(asyncResult)

    ' Asynchronously read back the written data.
    fStream.Position = 0
    asyncResult = fStream.BeginRead( _ 
        tempState.ReadArray, 0 , tempState.ReadArray.Length, _
        AddressOf EndReadCallback, tempState)

    ' Concurrently do other work, such as 
    ' logging the write operation.
End Sub

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

    // Asynchronously read back the written data.
    fStream.set_Position(0);
    asyncResult = fStream.BeginRead(tempState.get_ReadArray(), 0,
        tempState.get_ReadArray().length , 
        new AsyncCallback(EndReadCallback), tempState);

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

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Show: