PipeStream.WriteAsync 方法

定义

重载

WriteAsync(ReadOnlyMemory<Byte>, 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(Byte[], Int32, Int32, CancellationToken)

从字节数组的指定位置开始异步写入指定字节数,按写入的字节数向前移动此流中的当前位置,并监视取消请求。Asynchronously writes a specified number of bytes from a byte array starting at a specified position, 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 stream, advances the current position within this 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 = null);
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.

异常

流不支持写入。Stream does not support writing.

无法访问关闭的管道。Cannot access a closed pipe.

管道已断开。The pipe is broken.

管道尚未连接。The pipe hasn't been connected yet.

- 或 --or-

管道处于断开连接状态。The pipe is in a disconnected state.

- 或 --or-

尚未设置管道句柄。The pipe handle has not been set. (你的 PipeStream 实现是否调用 InitializeHandle(SafePipeHandle, Boolean, Boolean)(Did your PipeStream implementation call InitializeHandle(SafePipeHandle, Boolean, Boolean)?

注解

利用 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.

如果在操作完成前取消该操作,则返回的任务将包含 Status 属性的 Canceled 值。If the operation is canceled before it completes, the returned task contains the Canceled value for the Status property.

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

从字节数组的指定位置开始异步写入指定字节数,按写入的字节数向前移动此流中的当前位置,并监视取消请求。Asynchronously writes a specified number of bytes from a byte array starting at a specified position, 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. 默认值为 NoneThe default value is None.

返回

表示异步写入操作的任务。A task that represents the asynchronous write operation.

异常

buffernullbuffer is null.

offset 为负。The offset is negative.

- 或 --or-

count 为负。The count is negative.

buffer.Length - offset 小于 countbuffer.Length - offset is less than count.

流不支持写入。Stream does not support writing.

无法访问关闭的管道。Cannot access a closed pipe.

管道已断开。The pipe is broken.

管道尚未连接。The pipe hasn't been connected yet.

- 或 --or-

管道处于断开连接状态。The pipe is in a disconnected state.

- 或 --or-

尚未设置管道句柄。The pipe handle has not been set. (你的 PipeStream 实现是否调用 InitializeHandle(SafePipeHandle, Boolean, Boolean)(Did your PipeStream implementation call InitializeHandle(SafePipeHandle, Boolean, Boolean)?

注解

利用 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.

如果在操作完成前取消该操作,则返回的任务将包含 Status 属性的 TaskStatus.Canceled 值。If the operation is canceled before it completes, the returned task contains the TaskStatus.Canceled value for the Status property.

适用于