The recommended method for observing a cancellation request to cancel a response seems to be ThrowIfCancellationRequested .
But what happens if it is caught by a try-catch user? From the MSDN "How to Cancel a Task and Its Children" with the addition of try-catch to illustrate the issue:
snippet: static void DoSomeWork(int taskNum, CancellationToken ct) { try { for (int i = 0; i < maxIterations; i++) { // Do a bit of work. Not too much. ... //ok not to do this check? most likely IsCancellationRequested does it already //if (ct.IsCancellationRequested) //{ ct.ThrowIfCancellationRequested(); //} } } catch(OperationCanceledException e1) // catching likely my own exception { throw; // correct? anything else belongs here? } catch // ... { // do whatever else I might want to do here } }
Am I throwing well? That is, I do not violate anything in the task APIs, right?
(I will also express a personal opinion that the point of ordered cancel-clear-return seems contradictory, except that it is a means for this, I believe that there are other means for this - I will continue to dig)
source share