I have just taken .NET CopyTo implementation and added one line in the cycle:
public static void CopyTo(this Stream source, Stream destination, int bufferSize, CancellationToken cancellationToken)
{
var buffer = new byte[bufferSize];
int count;
while ((count = source.Read(buffer, 0, buffer.Length)) != 0)
{
cancellationToken.ThrowIfCancellationRequested();
destination.Write(buffer, 0, count);
}
}
But I find it disappointing .NET Framework designers don't care about cancellation of long-running synchronous operations.