void Do(CancellationToken cancellationToken, Profiler profiler) { bool Read(out T data) { bool finished = true; profiler?.BeginWatch(ProfilingType.Read); try { finished = inputChannel.Read(out data, profiler); } finally { profiler?.EndWatch(ProfilingType.Read); } return(finished); } U Transform(T data) { U result = default(U); profiler?.BeginWatch(ProfilingType.Transform); try { result = transform(data); } finally { profiler?.EndWatch(ProfilingType.Transform); } return(result); } void Write(U data) { profiler?.BeginWatch(ProfilingType.Write); try { outputChannel.Write(data); } finally { profiler?.EndWatch(ProfilingType.Write); } } while (Read(out T data)) { if (cancellationToken.IsCancelled) { break; } Write(Transform(data)); } }