You are implementing a method named ProcessData that performs a long-running task. The ProcessData() method has the following method signature: public void ProcessData(List
If the calling code requests cancellation, the method must perform the following actions:
Cancel the long-running task.
Set the task status to TaskStatus.Canceled.
You need to ensure that the ProcessData() method performs the required actions.
Which code segment should you use in the method body?
A. if (token.IsCancellationRequested) return;
B. throw new AggregateException();
C. token.ThrowIfCancellationRequested();
D. source.Cancel();
I believe that the question is referring to what you should do in the ProcessData method, not the calling method.
The caller would call source.Cancel(). In ProcessData, you would call token.ThrowIfCancellationRequested – as in this code:
https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-cancellation
The calling method (a lambda expression) checks IsCancellationRequested and then calls ThrowIfCancellationRequested. The caller calls tokenSource2.Cancel(). The lambda expression is the equivalent of ProcessData.
When the owning object calls CancellationTokenSource.Cancel, the IsCancellationRequested property on every copy of the cancellation token is set to true.
D
why isn’t it D?
I think so