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


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

 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



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


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


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


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


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


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


尝试的异步读取超过了流的结尾,或者发生了磁盘错误。Attempted an asynchronous read past the end of the stream, or a disk error occurred.

一个或多个自变量无效。One or more of the arguments is invalid.

在流关闭后调用方法。Methods were called after the stream was closed.

当前 Stream 实现不支持读取操作。The current Stream implementation does not support the read operation.


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

IAsyncResult 返回值传递到流的 EndRead 方法,以确定读取的字节数,以及释放用于读取的操作系统资源。Pass the IAsyncResult return value to the EndRead method of the stream to determine how many bytes were read and to release operating system resources used for reading. 对于每次调用 BeginRead,都必须调用 EndRead 一次。EndRead must be called once for every call to BeginRead. 为此,可以使用 BeginRead 调用的相同代码,也可以在传递给 BeginRead的回调中执行此操作。You can do this either by using the same code that called BeginRead or in a callback passed to BeginRead.

发出异步读取或写入操作时,将更新流中的当前位置,而不是在 i/o 操作完成时进行更新。The current position in the stream is updated when the asynchronous read or write is issued, not when the I/O operation completes.

多个同时进行的异步请求会导致不确定请求完成顺序。Multiple simultaneous asynchronous requests render the request completion order uncertain.

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

如果流已关闭或传递的参数无效,则会立即从 BeginRead引发异常。If a stream is closed or you pass an invalid argument, exceptions are thrown immediately from BeginRead. 在异步读取请求期间发生的错误(例如,i/o 请求过程中的磁盘故障)会在线程池线程上发生,并在调用 EndRead时引发异常。Errors that occur during an asynchronous read request, such as a disk failure during the I/O request, occur on the thread pool thread and throw exceptions when calling EndRead.