using System; using System.Threading; using System.Threading.Tasks; namespace EC.Onvif.RemoteDiscovery { internal static class ExtensionMethods { /// /// Used to provide cancellation possibility to any Async Methods returning a Task /// internal static async Task WithCancellation(this Task task, CancellationToken cancellationToken) { var tcs = new TaskCompletionSource(); using (cancellationToken.Register(s => ((TaskCompletionSource)s).TrySetResult(true), tcs)) { if (task != await Task.WhenAny(task, tcs.Task)) { throw new OperationCanceledException(cancellationToken); } } return await task; } } }