Created
September 28, 2018 15:37
-
-
Save GeorgeTsiokos/df70b4b84e841693f23fca65a2e3b75b to your computer and use it in GitHub Desktop.
New CancellationToken w/timeout
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
public static CancellationToken Timeout(this CancellationToken cancellationToken, TimeSpan timeout) | |
{ | |
if (!cancellationToken.CanBeCanceled) | |
return new CancellationTokenSource(timeout).Token; | |
var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); | |
cancellationTokenSource.CancelAfter(timeout); | |
return cancellationTokenSource.Token; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment