MemoryStream.ReadAsync 方法

定義

多載

ReadAsync(Memory<Byte>, CancellationToken)
ReadAsync(Byte[], Int32, Int32, CancellationToken)

以非同步的方式從目前資料流讀取一連串的位元組、依所讀取的位元組數目進階資料流中的位置,以及監視取消要求。Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.

ReadAsync(Memory<Byte>, CancellationToken)

public override System.Threading.Tasks.ValueTask<int> ReadAsync (Memory<byte> destination, System.Threading.CancellationToken cancellationToken = null);
override this.ReadAsync : Memory<byte> * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int>

參數

destination
Memory<Byte>
cancellationToken
CancellationToken

傳回

ReadAsync(Byte[], Int32, Int32, CancellationToken)

以非同步的方式從目前資料流讀取一連串的位元組、依所讀取的位元組數目進階資料流中的位置,以及監視取消要求。Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.

public:
 override System::Threading::Tasks::Task<int> ^ ReadAsync(cli::array <System::Byte> ^ buffer, int offset, int count, System::Threading::CancellationToken cancellationToken);
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task<int> ReadAsync (byte[] buffer, int offset, int count, System.Threading.CancellationToken cancellationToken);
override this.ReadAsync : byte[] * int * int * System.Threading.CancellationToken -> System.Threading.Tasks.Task<int>

參數

buffer
Byte[]

寫入資料的緩衝區。The buffer to write the data into.

offset
Int32

開始於此處自資料流寫入資料的 buffer 中的位元組位移。The byte offset in buffer at which to begin writing data from the stream.

count
Int32

要讀取的最大位元組數。The maximum number of bytes to read.

cancellationToken
CancellationToken

用來監視是否有取消要求的語彙基元。The token to monitor for cancellation requests. 預設值為 NoneThe default value is None.

傳回

表示非同步讀取作業的工作。A task that represents the asynchronous read operation. TResult 參數的值會包含讀取至緩衝區的位元組總數。The value of the TResult parameter contains the total number of bytes read into the buffer. 如果目前可供使用的位元組數目少於所要求的數目,結果值可能會小於所要求的位元組數目,或者如果已經到達資料流末端,則可能為 0 (零)。The result value can be less than the number of bytes requested if the number of bytes 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.

offsetcount 為負。offset or count is negative.

offsetcount 的總和大於緩衝區長度。The sum of offset and count is larger than the buffer length.

資料流不支援讀取。The stream does not support reading.

資料流已經處置。The stream has been disposed.

之前的讀取作業目前正在使用資料流。The stream is currently in use by a previous read operation.

備註

如果作業在完成前取消, 則傳回的工作會包含Canceled Status屬性的值。If the operation is canceled before it completes, the returned task contains the Canceled value for the Status property.

您可以建立CancellationTokenSource類別的實例, 並Token傳遞屬性作為cancellationToken參數, 藉以建立取消權杖。You can create a cancellation token by creating an instance of the CancellationTokenSource class and passing the Token property as the cancellationToken parameter.

適用於