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<decimal> 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();
D
C – token.ThrowIfCancellationRequested() sets TaskStatus.Canceled
No it does not
D
It’s C because “source.Cancel()” only “Communicates a request for cancellation.” as stated here https://msdn.microsoft.com/de-de/library/system.threading.cancellationtokensource(v=vs.110).aspx
without a line inside the Task like:
token.ThrowIfCancellationRequested() (Answer C)
OR
if(token.IsCancellationRequested)
{
return;
}
(2nd solution will not set the TaskStatus.Canceled)
Nothing will happen and the Thread will run to it’s end as you never had even called source.Cancel()
C.
must b c. source.cancel is callled by the calling method. operating method is free to ignore the request.
Mike explains it best.
token.ThrowIfCancellationRequested() (Answer C)
OR
if(token.IsCancellationRequested)
{
return;
}
(2nd solution will not set the TaskStatus.Canceled)