Which code segment should you use in the method body?

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 values, CancellationTokenSource source, CancellationToken token)
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();

microsoft-exams

4 thoughts on “Which code segment should you use in the method body?

  1. 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.

  2. When the owning object calls CancellationTokenSource.Cancel, the IsCancellationRequested property on every copy of the cancellation token is set to true.

    D

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.