Is there a way to do some sort of "aggregated" CancelationTokenSource in C #?

Is there a way to implement a CancelationTokenSource that allows two or more other CancelationToken to be aggregated internally? which allows me to write something like this:

void async Task DoSomeWorkAsync(CancelationToken Token)
{
    var cts = AggregatedCancelationTokenSource(Token, 
      new CancelationTokenSource(TimeSpan.FromSeconds(30)).Token);
    //Cancelation occurred:
    // when timeout will be expired 
    //  or 
    // when external Token.IsCancellationRequested will start to return "true"
    await DoSomeOtherWorkAsync(cts.Token);
}

      

+3


source to share


1 answer


Yes, you can use CancellationTokenSource.CreateLinkedTokenSource

, from the documentation :



Creates a CancellationTokenSource that will be in a canceled state when any of the source tokens are in a canceled state.

+2


source







All Articles