I'm using a CancellationTokenSource for two asynchronous actions, then awaiting them using Task.WhenAny
But this is triggering AsyncFixer (Code AsyncFixer04) in a way that I don't understand.
If I set the scope of the CancellationTokenSource with a using statement in brackets, like so
using System.Threading;
using System.Threading.Tasks;
namespace AsyncFixerIssue
{
internal class Program
{
static Task Main(string[] args)
{
using (var cts = new CancellationTokenSource())
{
var a = Task.Delay(1, cts.Token);
var b = Task.Delay(1, cts.Token);
return Task.WhenAny(a, b);
}
}
}
}
it triggers a warning from AsyncFixer04.
But if I format it like this, there's no problem
static Task Main(string[] args)
{
using var cts = new CancellationTokenSource();
var a = Task.Delay(1, cts.Token);
var b = Task.Delay(1, cts.Token);
return Task.WhenAny(a, b);
}
Also, I can just set cts.Token to a variable and make the analyzer happy
static Task Main(string[] args)
{
using (var cts = new CancellationTokenSource())
{
var cancellationToken = cts.Token;
var a = Task.Delay(1, cancellationToken);
var b = Task.Delay(1, cancellationToken);
return Task.WhenAny(a, b);
}
}
It seems like either all of these should trigger the analyzer, or none of them should.
Is this a bug?
If not, what's your recommended fix for passing along a CancellationTokenSource?
I'm using a CancellationTokenSource for two asynchronous actions, then awaiting them using Task.WhenAny
But this is triggering AsyncFixer (Code AsyncFixer04) in a way that I don't understand.
If I set the scope of the CancellationTokenSource with a using statement in brackets, like so
it triggers a warning from AsyncFixer04.
But if I format it like this, there's no problem
Also, I can just set
cts.Tokento a variable and make the analyzer happyIt seems like either all of these should trigger the analyzer, or none of them should.
Is this a bug?
If not, what's your recommended fix for passing along a CancellationTokenSource?