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.

适用于