BufferedStream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) 方法

定义

开始执行异步读取操作。Begins an asynchronous read operation. (请考虑改用 ReadAsync(Byte[], Int32, Int32, CancellationToken)。)(Consider using ReadAsync(Byte[], Int32, Int32, CancellationToken) instead.)

public:
 override IAsyncResult ^ BeginRead(cli::array <System::Byte> ^ buffer, int offset, int count, AsyncCallback ^ callback, System::Object ^ state);
public override IAsyncResult BeginRead (byte[] buffer, int offset, int count, AsyncCallback callback, object state);
override this.BeginRead : byte[] * int * int * AsyncCallback * obj -> IAsyncResult
Public Overrides Function BeginRead (buffer As Byte(), offset As Integer, count As Integer, callback As AsyncCallback, state As Object) As IAsyncResult

参数

buffer
Byte[]

要将数据读入的缓冲区。The buffer to read the data into.

offset
Int32

buffer 中的字节偏移量,从该偏移量开始写入从流中读取的数据。The byte offset in buffer at which to begin writing data read from the stream.

count
Int32

要读取的最大字节数。The maximum number of bytes to read.

callback
AsyncCallback

可选的异步回调,将在读取完成时调用。An optional asynchronous callback, to be called when the read is complete.

state
Object

用户提供的对象,用于将此特定异步读取请求与其他请求区分开来。A user-provided object that distinguishes this particular asynchronous read request from other requests.

返回

表示异步读取(可能仍处于挂起状态)的对象。An object that represents the asynchronous read, which could still be pending.

异常

buffernullbuffer is null.

offsetcount 为负数。offset or count is negative.

试图在流的末尾之外进行异步读取。Attempted an asynchronous read past the end of the stream.

缓冲区长度减去 offset 小于 countThe buffer length minus offset is less than count.

当前流不支持读取操作。The current stream does not support the read operation.

注解

在 .NET Framework 4 及更早版本中,必须使用 BeginReadEndRead 等方法来实现异步文件操作。In the .NET Framework 4 and earlier versions, you have to use methods such as BeginRead and EndRead to implement asynchronous file operations. 这些方法在 .NET Framework 4.5.NET Framework 4.5 仍可用以支持旧版代码;但是,新的异步方法(如 ReadAsyncWriteAsyncFlushAsync)可帮助你更轻松地实现异步文件操作。These methods are still available in the .NET Framework 4.5.NET Framework 4.5 to support legacy code; however, the new async methods, such as ReadAsync, WriteAsync, and FlushAsync, help you implement asynchronous file operations more easily.

对于每个调用 BeginRead,必须只调用一次 EndWriteEndWrite 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.

备注

使用 CanRead 属性可确定当前实例是否支持读取。Use the CanRead property to determine whether the current instance supports reading.

若要确定读取的字节数,必须用此 IAsyncResult 调用 EndReadEndRead must be called with this IAsyncResult to find out how many bytes were read.

适用于