FileStream.CopyToAsync(Stream, Int32, CancellationToken) 方法
定义
使用指定的缓冲区大小和取消令牌,从当前文件流中异步读取字节并将其写入到另一个流中。Asynchronously reads the bytes from the current file stream and writes them to another stream, using a specified buffer size and cancellation token.
public:
override System::Threading::Tasks::Task ^ CopyToAsync(System::IO::Stream ^ destination, int bufferSize, System::Threading::CancellationToken cancellationToken);
public override System.Threading.Tasks.Task CopyToAsync (System.IO.Stream destination, int bufferSize, System.Threading.CancellationToken cancellationToken);
override this.CopyToAsync : System.IO.Stream * int * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Overrides Function CopyToAsync (destination As Stream, bufferSize As Integer, cancellationToken As CancellationToken) As Task
参数
- destination
- Stream
向其中复制当前文件流的内容的流。The stream to which the contents of the current file stream will be copied.
- bufferSize
- Int32
缓冲区的大小(以字节为单位)。The size, in bytes, of the buffer. 此值必须大于零。This value must be greater than zero. 默认大小为 81920。The default size is 81920.
- cancellationToken
- CancellationToken
要监视取消请求的标记。The token to monitor for cancellation requests. 默认值为 None。The default value is None.
返回
表示异步复制操作的任务。A task that represents the asynchronous copy operation.
注解
CopyToAsync方法使你可以在不阻塞主线程的情况下执行占用大量资源的 i/o 操作。The CopyToAsync method enables you to perform resource-intensive I/O operations without blocking the main thread. 在 Windows 8.x 应用商店应用或桌面应用中一个耗时的流操作可能阻塞 UI 线程并让应用看起来好像不工作时,这种性能的考虑就显得尤为重要了。This performance consideration is particularly important in a 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. 异步方法与 async await Visual Basic 和 c # 中的和关键字结合使用。The async methods are used in conjunction with the async and await keywords in Visual Basic and C#.
如果在操作完成前取消该操作,则返回的任务包含 TaskStatus.Canceled 属性的值 Task.Status 。If the operation is canceled before it completes, the returned task contains the TaskStatus.Canceled value for the Task.Status property.
复制从当前文件流中的当前位置开始。Copying begins at the current position in the current file stream.
有关在两个流之间进行复制的示例,请参阅 CopyToAsync(Stream) 重载。For an example of copying between two streams, see the CopyToAsync(Stream) overload.