CancellationTokenSource.CreateLinkedTokenSource does not have an overload with TimeProvider #95822
-
I often have code that does something like: async Task MyAmazingLogic(CancellationToken cancellationToken)
{
using var timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
timeoutCts.CancelAfter(TimeSpan.FromSeconds(10));
// Invoke API's
await InvokeOtherStuff(timeoutCts.Token);
} I wanted to use this together with It seems I can only do: TimeProvider timeProvider;
async Task MyAmazingLogic(CancellationToken cancellationToken)
{
using var timeoutCts = new CancellationTokenSource(Timeout, timeProvider);
using var combinedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCts.Token);
// Invoke API's
await InvokeOtherStuff(combinedCts.Token);
} It would be nice if TimeProvider timeProvider;
async Task MyAmazingLogic(CancellationToken cancellationToken)
{
using var timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, TimeSpan.FromSeconds(10), timeProvider);
// Invoke API's
await InvokeOtherStuff(timeoutCts.Token);
} Did I miss any API's? Is the "double" CTS creation the only way to do this with .net 8? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
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);
} |
Beta Was this translation helpful? Give feedback.
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):