Created
April 22, 2021 04:38
-
-
Save crozone/61f4f5fde8e7de8c3ad65543195f5212 to your computer and use it in GitHub Desktop.
CancellationToken.WhenCancelled()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading; | |
using System.Threading.Tasks; | |
public static class CancellationTokenExtensions | |
{ | |
public static async Task WhenCancelled(this CancellationToken cancellationToken) | |
{ | |
TaskCompletionSource<bool> taskCompletionSource = new TaskCompletionSource<bool>(); | |
using (cancellationToken.Register(() => | |
{ | |
taskCompletionSource.TrySetResult(true); | |
})) | |
{ | |
await taskCompletionSource.Task; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment