You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

25 lines
695 B

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