MemoryStream.ReadAsync 方法

定義

多載

ReadAsync(Memory<Byte>, CancellationToken)

以非同步方式從目前的記憶體資料流讀取位元組序列、將此序列寫入至 destination、將記憶體資料流中的位置依讀取的位元組數向前移動,並監視取消要求。Asynchronously reads a sequence of bytes from the current memory stream, writes the sequence into destination, advances the position within the memory stream by the number of bytes read, and monitors cancellation requests.

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)

以非同步方式從目前的記憶體資料流讀取位元組序列、將此序列寫入至 destination、將記憶體資料流中的位置依讀取的位元組數向前移動,並監視取消要求。Asynchronously reads a sequence of bytes from the current memory stream, writes the sequence into destination, advances the position within the memory stream by the number of bytes read, and monitors cancellation requests.

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>

要寫入資料的記憶體區域。The region of memory to write the data into.

cancellationToken
CancellationToken

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

傳回

表示非同步讀取作業的工作。A task that represents the asynchronous read operation. Result 屬性值包含讀入至 destination 的位元組總數。The value of its Result property contains the total number of bytes read into the destination. 如果目前無法取得該數額的位元組,則結果值可能小於 destination 中配置的位元組數,如果已經到達記憶體資料流結尾,則為 0 (零)。The result value can be less than the number of bytes allocated in destination if that many bytes are not currently available, or it can be 0 (zero) if the end of the memory stream has been reached.

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.

備註

如果作業在完成前取消,則傳回的工作會包含 Status 屬性的 Canceled 值。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.

適用於