protected Task[] CopyToAsyncBoth(Stream remoteStream) { var psSafe = new SafeAsyncStream(this.ProxyStream); var rsSafe = new SafeAsyncStream(remoteStream); var factory = new TaskFactory(); return(new Task[] { CopyToAsync(factory, psSafe, rsSafe, this.CancelSource.Token).ContinueWith(Finalize), CopyToAsync(factory, rsSafe, psSafe, this.CancelSource.Token).ContinueWith(Finalize), }); void Finalize(Task task) { try { this.CancelSource.Cancel(); } catch { } psSafe.Dispose(); rsSafe.Dispose(); } }
protected Task CopyToAsync(Stream dst, Stream src) { var srcSafe = new SafeAsyncStream(src); var dstSafe = new SafeAsyncStream(dst); return(srcSafe.CopyToAsync(dstSafe, CopyToBufferSize, this.CancelSource.Token) .ContinueWith( e => { srcSafe.Dispose(); dstSafe.Dispose(); if (!e.IsFaulted && !e.IsCanceled) { this.CancelSource.Cancel(); } })); }