using System.Threading; using System.Threading.Tasks; namespace GISTech.GISDataDownloader { public static class TaskCancellationExtension { public static Task WhenCanceled(this CancellationToken cancellationToken) { TaskCompletionSource taskCompletionSource = new TaskCompletionSource(); cancellationToken.Register(delegate(object s) { ((TaskCompletionSource)s).SetResult(result: true); }, taskCompletionSource); return taskCompletionSource.Task; } public static Task CancelWith(this Task task, CancellationToken cancellationToken, bool swallowCancellationException = false) { return TaskCancellationInternals.CancelWithInternal(task, cancellationToken, swallowCancellationException); } public static Task CancelWith(this Task task, CancellationToken cancellationToken, string message, bool swallowCancellationException = false) { return TaskCancellationInternals.CancelWithInternal(task, cancellationToken, message, swallowCancellationException); } public static Task CancelWith(this Task task, CancellationToken cancellationToken, bool swallowCancellationException = false) { return TaskCancellationInternals.CancelWithInternal(task, cancellationToken, swallowCancellationException); } } }