Which two actions should you perform?

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 so
that the UI can be updated with the new values.
You need to ensure that the calling code handles the long-running operation as a
System.Threading.Tasks.Task object to avoid blocking the UI thread.
Which two actions should you perform? (Each correct answer presents part of the solution.
Choose two.)

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 so
that the UI can be updated with the new values.
You need to ensure that the calling code handles the long-running operation as a
System.Threading.Tasks.Task object to avoid blocking the UI thread.
Which two actions should you perform? (Each correct answer presents part of the solution.
Choose two.)

A.
Create a TaskCompletionSource<T> object.

B.
Call the component by using the TaskFactory.FromAsync() method.

C.
Apply the following attribute to the ProcessData() method signature:
[Methodlmpl(MethodlmplOptions.Synchronized)]

D.
Apply the async modifier to the ProcessData() method signature.

Explanation:
A: In many scenarios, it is useful to enable a Task<TResult> to represent an external asynchronous operation.
TaskCompletionSource<TResult> is provided for this purpose. It enables the creation of a task that can be
handed out to consumers, and those consumers can use the members of the task as they would any other.
However, unlike most tasks, the state of a task created by a TaskCompletionSource is controlled explicitly by
the methods on TaskCompletionSource. This enables the completion of the external asynchronous operation to
be propagated to the underlying Task. The separation also ensures that consumers are not able to transition
the state without access to the corresponding TaskCompletionSource.
B: TaskFactory.FromAsync Method
Creates a Task that represents a pair of begin and end methods that conform to the Asynchronous
Programming Model pattern. Overloaded.
Example:
TaskFactory.FromAsync Method (IAsyncResult, Action<IAsyncResult>)
Creates a Task that executes an end method action when a specified IAsyncResult completes.
Note:
* System.Threading.Tasks.Task
Represents an asynchronous operation.



Leave a Reply 0

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