// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Threading.Tasks; namespace System.IO.Pipelines { public static class PipeCompletionExtensions { public static Task WaitForWriterToComplete(this PipeReader reader) { var tcs = new TaskCompletionSource(); reader.OnWriterCompleted((ex, state) => { if (ex != null) { ((TaskCompletionSource)state).TrySetException(ex); } else { ((TaskCompletionSource)state).TrySetResult(null); } }, tcs); return tcs.Task; } public static Task WaitForReaderToComplete(this PipeWriter writer) { var tcs = new TaskCompletionSource(); writer.OnReaderCompleted((ex, state) => { if (ex != null) { ((TaskCompletionSource)state).TrySetException(ex); } else { ((TaskCompletionSource)state).TrySetResult(null); } }, tcs); return tcs.Task; } } }