The reason is that if it is CancellationToken.None, I need to add a timeout (so the new CancellationToken from CancellationTokenSource), for example
if (cancellationToken == CancellationToken.None)
{
var tokenSource = new CancellationTokenSource();
tokenSource.CancelAfter((int)_timeout.TotalMilliseconds);
cancellationToken = tokenSource.Token;
}
Another idea is to create a combined token.
Update. Will there be something like this work? Any issues I should know about? Will the GC do what it needs?
public async Task<int> InternalActionAsync(CancellationToken token)
{
return 1;
}
public Task<int> ExternalActionAsync(CancellationToken token = default(CancellationToken))
{
var internalTokenSource =
new CancellationTokenSource((int)_timeout.TotalMilliseconds);
var linkedTokenSource =
CancellationTokenSource.CreateLinkedTokenSource(token,
internalTokenSource.Token);
return InternalActionAsync(linkedTokenSource.Token);
}
source
share