I have a piece of code which is doing something like:
CancellationTokenSource _myCts;
private async Task RunAsync()
{
using (_myCts = CancellationTokenSource.CreateLinkedTokenSource(token1, token2))
{
await DoWork();
}
}
public void CancelOperation()
{
if (_myCts != null)
{
_myCts.Cancel();
}
}
It is correct this code? I am afraid that the Cancel() method of the cancellation token may be executed after disposed. It could be that the if checks the non-nullability of it, and right after that the using statement finishes.
Thank you