I'm using CancellationTokenSource
for timeout purposes most of the time. To avoid forgetting dispose
, when I'm done with the CancellationTokenSource
, I'm using a using
statement. But before the end of the using
statement, I'm always doing a CancellationTokenSource.Cancel()
.
Is it necessary to cancel CancellationTokenSource
before disposal if the cancellation hasn't been used?
Here is an example of code where I'm doing this.
using (CancellationTokenSource TokenSource = new CancellationTokenSource(nTimeout * 1000))
{
for (int i = 0; i < nLoop; i++)
{
if (TokenSource.Token.IsCancellationRequested)
{
bSuccess = false;
break;
}
await Task.Delay(cDelay);
// do some work
}
TokenSource.Cancel();
}