Skip to content

CancellationTokenSource.CreateLinkedTokenSource does not have an overload with TimeProvider #95822

Answered by stephentoub
ramonsmits asked this question in Q&A
Discussion options

You must be logged in to vote

Is the "double" CTS creation the only way to do this with .net 8?

Or just do the registration yourself, since that's really all CreateLinkedTokenSource does for you (including disposing of the registration when you dispose the CTS):

async Task MyAmazingLogic(CancellationToken cancellationToken)
{
    using var cts = new CancellationTokenSource(Timeout, timeProvider);
    using var _ = cancelationToken.Register(s => ((CancellationTokenSource)s).Cancel(), cts);

    // Invoke API's
    await InvokeOtherStuff(cts.Token);
}

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@ramonsmits
Comment options

@stephentoub
Comment options

Answer selected by ramonsmits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants