Question 2) Development Foundation (VB.NET)
Objective: Implementing Service Processes, Threading, and Application Domains in a .NET Framework Application
SubObjective: Develop multithreaded .NET Framework applications (Refer System.Threading namespace)
Single Answer Multiple Choice
You are an application developer for a company. You create an application that contains the following code:
‘Delegate
Public Delegate Function GetFileContentsDel() As String
Public Function GetFileContents() As String
‘Process file and return results
End Sub
You want to invoke this method asynchronously. After you invoke the GetFileContents method, you will need to continue processing other user instructions. The results should be displayed as soon as the GetFileContents method finishes processing.
Which code should you use to invoke the GetFileContents method asynchronously?
A. Dim delAsync As New GetFileContentsDel(AddressOf GetFileContents)
Dim strFile As String = delAsync.Invoke()
B. Dim strFile As String = GetFileContents.Invoke()
C. Dim delAsync As New GetFileContentsDel(AddressOf GetFileContents)
Dim result As IAsyncResult = _
delAsync.BeginInvoke(Nothing, Nothing)
‘Process other user instructions
Dim strFile As String = delAsync.EndInvoke(result)
D. Dim delAsync As New GetFileContentsDel(AddressOf GetFileContents)
Dim result As IAsyncResult = _
delAsync.BeginInvoke(Nothing, Nothing)
While Not result.IsCompleted
‘Process other user instructions
End While
Dim strFile As String = delAsync.EndInvoke(result)
Answer:
D. Dim delAsync As New GetFileContentsDel(AddressOf GetFileContents)
Dim result As IAsyncResult = _
delAsync.BeginInvoke(Nothing, Nothing)
While Not result.IsCompleted
‘Process other user instructions
End While
Dim strFile As String = delAsync.EndInvoke(result)
Tutorial:
You should use the following code to invoke the GetFileContents method asynchronously:
Dim delAsync As New GetFileContentsDel(AddressOf GetFileContents)
Dim result As IAsyncResult = _
delAsync.BeginInvoke(Nothing, Nothing)
While Not result.IsCompleted
‘Process other user instructions
End While
Dim strFile As String = delAsync.EndInvoke(result)
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 AsyncCallback delegate and a generic object. The AsyncCallback delegate references the method that the worker thread will invoke when its processing is complete. In this scenario, no AsyncCallback delegate is specified. Then, the code polls the IAsyncResult object to determine if its processing is complete using the IsCompleted property. After the processing is complete, the loop is exited and the EndInvoke method returns the result from the GetFileContents method.
You should not use either of the code fragments that use the Invoke method because this is not a technique in asynchronous processing. You would use the Invoke method in synchronous programming only. Also, one of these code fragments does not instantiate the delegate and attempts to invoke the Invoke method explicitly on the GetFileContents method. If used, this will cause a compile-time error.
You should not use the code that does not poll the IAsyncResult object by retrieving the IsCompleted property. You are required to continue processing other user instructions only while waiting for the GetFileContents method to complete its processing.