MemoryStream.WriteAsync 方法

定義

多載

WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)

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

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

以非同步的方式將一連串的位元組寫入目前的資料流,由這個資料流中目前的位置前移寫入的位元組數目,並且監視取消要求。Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)

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

public override System.Threading.Tasks.ValueTask WriteAsync (ReadOnlyMemory<byte> source, System.Threading.CancellationToken cancellationToken = null);
override this.WriteAsync : ReadOnlyMemory<byte> * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask

參數

source
ReadOnlyMemory<Byte>

資料寫入來源的記憶體區域。The region of memory to write data from.

cancellationToken
CancellationToken

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

傳回

表示非同步寫入作業的工作。A task that represents the asynchronous write operation.

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

以非同步的方式將一連串的位元組寫入目前的資料流,由這個資料流中目前的位置前移寫入的位元組數目,並且監視取消要求。Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

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

參數

buffer
Byte[]

寫入資料的來源緩衝區。The buffer to write data from.

offset
Int32

buffer 中以零起始的位元組位移,要從其中開始將位元組複製至資料流。The zero-based byte offset in buffer from which to begin copying bytes to the stream.

count
Int32

寫入的最大位元組數。The maximum number of bytes to write.

cancellationToken
CancellationToken

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

傳回

表示非同步寫入作業的工作。A task that represents the asynchronous write operation.

屬性

例外狀況

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 writing.

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

之前的寫入作業目前正在使用資料流。The stream is currently in use by a previous write 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.

適用於