PrepAway - Latest Free Exam Questions & Answers

What should you do?

You have recently created an application that includes the code shown below.
public delegate string GetFileContentsDel ();
public string GetFileContents () {
//Process file and return results
}
You now need to invoke the GetFileContents method asynchronously.
You have to ensure that the code you use to invoke the GetFileContents method will continue to process other user instructions, and displays the results as soon as the GetFileContents method finishes processing. What should you do?

PrepAway - Latest Free Exam Questions & Answers

A.
Use the following code:
GetFileContentsDel delAsync = new GetFileContentsDel (GetFileContents);
IAsyncResult result = delAsync.BeginInvoke (null, null);
while (!result.IsCompleted) {
//Process other user instructions
}
string strFile = delAsync.EndInvoke (result);

B.
Use the following code:
GetFileContentsDel delAsync = new GetFileContentsDel (GetFileContents);
string strFile = delAsync.Invoke ();

C.
Use the following code:
string strFile = GetFileContents.Invoke ();

D.
Use the following code:
GetFileContentsDel delAsync = new GetFileContentsDel (GetFileContents);
IAsyncResult result = delAsync.BeginInvoke (null, null);
//Process other user instructions
string strFile = delAsync.EndInvoke (result)

Explanation:
This code instantiates a GetFileContentsDel delegate that references the GetFileContents method. Then, the BeginInvoke method is invoked to implicitly create and start the worker thread. The BeginInvoke method takes the same arguments as the method it references but also includes an AsyncCallack delegate and a generic object. The AsyncCallack delegate references the method that the worker thread will invoke when its processing is complete. In this scenario, there is no AsyncCallack delegate specified. Then, the code polls the IAsyncResult object to determine if it’s processing is complete using the IsCompleted property. Once the processing is complete, the loop is exited and the EndInvoke method returns the result from the GetFileContents method.
Incorrect Answers:
B, C: You should not use either of the code fragments that use the Invoke method because this is not a technique in asynchronous processing.
D: you should not use the code that does not poll the IAsyncResult object by retrieving the IsCompleted property.

One Comment on “What should you do?


Leave a Reply