StreamReader.ReadBlockAsync 方法

定义

重载

ReadBlockAsync(Memory<Char>, CancellationToken)

从当前流中异步读取字符并将数据写入缓冲区。Asynchronously reads the characters from the current stream and writes the data to a buffer.

ReadBlockAsync(Char[], Int32, Int32)

从当前流中异步读取指定的最大字符,并且从指定的索引位置开始将该数据写入缓冲区。Reads a specified maximum number of characters from the current stream asynchronously and writes the data to a buffer, beginning at the specified index.

ReadBlockAsync(Memory<Char>, CancellationToken)

从当前流中异步读取字符并将数据写入缓冲区。Asynchronously reads the characters from the current stream and writes the data to a buffer.

public override System.Threading.Tasks.ValueTask<int> ReadBlockAsync (Memory<char> buffer, System.Threading.CancellationToken cancellationToken = default);
override this.ReadBlockAsync : Memory<char> * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int>
Public Overrides Function ReadBlockAsync (buffer As Memory(Of Char), Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Integer)

参数

buffer
Memory<Char>

此方法返回时,包含指定的字符内存块,这些字符由从当前源中读取的字符替换。When this method returns, contains the specified memory block of characters replaced by the characters read from the current source.

cancellationToken
CancellationToken

要监视取消请求的标记。The token to monitor for cancellation requests. 默认值是 NoneThe default value is None.

返回

ValueTask<Int32>

表示异步读取操作的值任务。A value task that represents the asynchronous read operation. 值任务的类型参数的值包含读入缓冲区的总字符数。The value of the type parameter of the value task contains the total number of characters read into the buffer. 如果当前可用字符数少于所请求的字符数,则该结果值可小于所请求的字符数;如果已到达流结尾时,则为 0(零)。The result value can be less than the number of characters requested if the number of characters currently available is less than the requested number, or it can be 0 (zero) if the end of the stream has been reached.

适用于

ReadBlockAsync(Char[], Int32, Int32)

从当前流中异步读取指定的最大字符,并且从指定的索引位置开始将该数据写入缓冲区。Reads a specified maximum number of characters from the current stream asynchronously and writes the data to a buffer, beginning at the specified index.

public:
 override System::Threading::Tasks::Task<int> ^ ReadBlockAsync(cli::array <char> ^ buffer, int index, int count);
public override System.Threading.Tasks.Task<int> ReadBlockAsync (char[] buffer, int index, int count);
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task<int> ReadBlockAsync (char[] buffer, int index, int count);
override this.ReadBlockAsync : char[] * int * int -> System.Threading.Tasks.Task<int>
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.ReadBlockAsync : char[] * int * int -> System.Threading.Tasks.Task<int>
Public Overrides Function ReadBlockAsync (buffer As Char(), index As Integer, count As Integer) As Task(Of Integer)

参数

buffer
Char[]

当此方法返回时,包含指定的字符数组,此数组中 index 和 (index + count - 1) 之间的值被从当前源中读取的字符所替换。When this method returns, contains the specified character array with the values between index and (index + count - 1) replaced by the characters read from the current source.

index
Int32

buffer 中开始写入的位置。The position in buffer at which to begin writing.

count
Int32

最多读取的字符数。The maximum number of characters to read. 如果在将指定的字符数写入到缓冲区之前就已经达到流结尾,则将返回此方法。If the end of the stream is reached before the specified number of characters is written into the buffer, the method returns.

返回

Task<Int32>

表示异步读取操作的任务。A task that represents the asynchronous read operation. TResult 参数的值包含读入缓冲区的总字符数。The value of the TResult parameter contains the total number of characters read into the buffer. 如果当前可用字符数少于所请求的字符数,则该结果值可小于所请求的字符数;如果已到达流结尾时,则为 0(零)。The result value can be less than the number of characters requested if the number of characters currently available is less than the requested number, or it can be 0 (zero) if the end of the stream has been reached.

属性

例外

buffernullbuffer is null.

indexcount 为负数。index or count is negative.

indexcount 的总和大于缓冲区长度。The sum of index and count is larger than the buffer length.

已释放流。The stream has been disposed.

以前的读取操作当前正在使用读取器。The reader is currently in use by a previous read operation.

注解

在读取参数指定的字符数 count 或已到达流的末尾之前,任务不会完成。The task does not complete until either the number of characters specified by the count parameter are read, or the end of the stream has been reached.

适用于