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.
20 lines
700 B
20 lines
700 B
2 years ago
|
namespace EC.Helper.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;
|
||
|
}
|
||
|
}
|