C#C
C#3y ago
Hugh

✅ Creating new cancellation token from multiple other tokens

I've got function that is being passed in a cancellation token, and I want to cancel an inner function call if either this cancellation token is triggered, or another internal one is triggered (due to a timeout).

My method looks something like this:

public async Task DoSomethingAsync(TimeSpan timeout, CancellationToken cancellationToken)
{
  CancellationTokenSource timeoutCancellationTokenSource = new();
  timeoutCancellationTokenSource.CancelAfter(timeout);

  // Create someCombinedCancellationToken which will cancel if either cancellationToken is cancelled
  // or timeoutCancellationTokenSource is triggered

  await DoSomethingElseAsync(someCombinedCancellationToken);
}


If there's no way of doing this, then I am thinking that I could do it something like this:

public async Task DoSomethingAsync(TimeSpan timeout, CancellationToken cancellationToken)
{
  CancellationTokenSource timeoutCancellationTokenSource = new();
  timeoutCancellationTokenSource.CancelAfter(timeout);

  Task result = DoSomethingElseAsync(timeoutCancellationTokenSource);
  while(!result.IsCompleted)
  {
    if(cancellationToken.IsCancellationRequested)
    {
      timeoutCancellationTokenSource.Cancel();
    }

    await Task.Delay(100);
  }
}


But it would be nice to know if there's a way of doing this with a combination cancellation token
Was this page helpful?