An OperationCanceledException could be thrown in a couple of different reasons:
- The cancelation token has been set, or
- There has been a timeout (TaskCancelationException)
To get around this, a common workaround (described here, et al: Distinguish timeout from user cancellation) is as follows:
catch (OperationCanceledException ex)
{
if (token.IsCancellationRequested)
{
return -1;
}
return -2;
}
But it would make more sense for me to look at the actual cancelation state that was given to the exception. Something like this:
if (ex is OperationCanceledException ocEx)
{
if (ocEx.CancellationToken.IsCancellationRequested)
{
return -1;
}
return -2;
}
Am I missing something, is there some reason this is not promoted as a solution for this common problem?