BufferedStream.WriteAsync 方法

定義

多載

WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)

以非同步方式將位元組序列寫入至目前的緩衝資料流、依寫入的位元組數將此緩衝資料流中目前位置往前移,並監視取消要求。Asynchronously writes a sequence of bytes to the current buffered stream, advances the current position within this buffered 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)

以非同步方式將位元組序列寫入至目前的緩衝資料流、依寫入的位元組數將此緩衝資料流中目前位置往前移,並監視取消要求。Asynchronously writes a sequence of bytes to the current buffered stream, advances the current position within this buffered stream by the number of bytes written, and monitors cancellation requests.

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

參數

buffer
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 方法可讓您執行耗用大量資源的 i/o 作業,而不會封鎖主執行緒。The WriteAsync method enables you to perform resource-intensive I/O operations without blocking the main thread. 這項效能考量對於 Windows 8.x 市集Windows 8.x Store 應用程式或 桌面desktop 應用程式而言特別重要,尤其是針對耗時的資料流作業可能會阻礙 UI 執行緒,使應用程式看起來像是停止運作的情況。This performance consideration is particularly important in a Windows 8.x 市集Windows 8.x Store app or 桌面desktop app where a time-consuming stream operation can block the UI thread and make your app appear as if it is not working. 非同步方法會與 Visual Basic 和C#中的 asyncawait 關鍵字搭配使用。The async methods are used in conjunction with the async and await keywords in Visual Basic and C#.

使用 CanWrite 屬性來判斷目前的實例是否支援寫入。Use the CanWrite property to determine whether the current instance supports writing.

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

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);
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.

傳回

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

例外狀況

buffer 上所宣告的預設值是 nullbuffer 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.

備註

您可以建立 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.

適用於