Programming in C# — Question 111
You are implementing a new method named ProcessData. The ProcessData() method calls a third-party component that performs a long-running operation to retrieve stock information from a web service.
The third-party component uses the IAsyncResult pattern to signal completion of the long-running operation.
You need to ensure that the calling code handles the long-running operation as a System.Threading.Tasks.Task object.
Which two actions should you perform? (Each correct answer presents part of the solution. Choose two.)
Answer options
- A. Call the component by using the TaskFactory.FromAsync() method.
- B. Create a TaskCompletionSource<T> object.
- C. Apply the async modifier to the method signature.
- D. Apply the following attribute to the method signature: [MethodImpl(MethodImplOptions.Synchronized)]
Correct answer: A, B
Explanation
The correct answers, A and B, are necessary because TaskFactory.FromAsync() is designed to wrap the IAsyncResult pattern into a Task, and TaskCompletionSource<T> is used to create a Task that can be completed based on the completion of the async operation. Options C and D are not relevant for converting the IAsyncResult pattern to a Task; the async modifier and the Synchronized attribute do not address the required pattern conversion.