Implementing Asynchronous Programming Techniques to Improve the User Experience

Posted on

These questions are based on 70-526VB: TS: Microsoft .NET Framework 2.0 – Windows-Based Client Development (VB.NET) Microsoft Self Test Software Practice Test.

 

Objective: Implementing Asynchronous Programming Techniques to Improve the User Experience
Sub-Objective: Manage a background process by using the BackgroundWorker component

 

Single Answer, Multiple Choice

 

You are an application developer for a company. You are creating a Windows application that will be used by employees of the sales and marketing departments to generate regional monthly sales reports. The sales data is stored in a Microsoft SQL Server 2005 database named Sales. The total number of records to be retrieved from the Sales database is displayed using a function named FetchMonthData. The FetchMonthData function accepts an argument named m_Month of Integer type. Users will specify the m_Month value using a TextBox control named txtMonth.

 

When your application is deployed in your production environment, users complain that reports using the FetchMonthData function take a long time to display and that the UI hangs.

 

You decide to execute the FetchMonthData asynchronously and add a BackgroundWorker component named BackgroundWorker1. You then add the following code to start the asynchronous operation:

 

Private Sub btnDisplayReport_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs)Handles btnDisplayReport.Click
BackgroundWorker1.RunWorkerAsync(txtMonth.Text)
End Sub

 

Private Sub backgroundWorker1_DoWork(ByVal sender As Object, _
ByVal e As DoWorkEventArgs)Handles BackgroundWorker1.DoWork
Dim myWork As BackgroundWorker = CType(sender, BackgroundWorker)
e.Result = FetchMonthData(e.Argument, myWork, e)
End Sub

 

You must display the result of FetchMonthData in a Label control named lblCount.

 

Which code should you use?

 

 

  1. Private Sub BackgroundWorker1_RunWorkerCompleted( _
    ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) _
    Handles BackgroundWorker1.RunWorkerCompleted
    If (e.Error IsNot Nothing) Then
    MessageBox.Show(e.Error.Message)
    Else
    lblCount.Text = e.Result.ToString()
    End If
    End Sub
  2. Private Sub BackgroundWorker1_ProgressChanged( _
    ByVal sender As Object, ByVal e As ProgressChangedEventArgs) _
    Handles BackgroundWorker1.ProgressChanged
    If (e.Error IsNot Nothing) Then
    MessageBox.Show(e.Error.Message)
    Else
    lblCount.Text = e.Result.ToString()
    End If
    End Sub
  3. Private Sub BackgroundWorker1_RunWorkerCompleted( _
    ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) _
    Handles BackgroundWorker1.RunWorkerCompleted
    If (e.Error IsNot Nothing) Then
    MessageBox.Show(e.Error.Message)
    Else
    lblCount.Text = BackgroundWorker1.Result.ToString()
    End If
    End Sub
  4. Private Sub BackgroundWorker1_ProgressChanged( _
    ByVal sender As Object, ByVal e As ProgressChangedEventArgs) _
    Handles BackgroundWorker1.ProgressChanged
    If (e.Error IsNot Nothing) Then
    MessageBox.Show(e.Error.Message)
    Else
    lblCount.Text = BackgroundWorker1.Result.ToString()
    End If
    End Sub

 

Answer:
A. Private Sub BackgroundWorker1_RunWorkerCompleted( _
ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) _
Handles BackgroundWorker1.RunWorkerCompleted
If (e.Error IsNot Nothing) Then
MessageBox.Show(e.Error.Message)
Else
lblCount.Text = e.Result.ToString()
End If
End Sub

 

Tutorial:
You should use the following code:

Private Sub BackgroundWorker1_RunWorkerCompleted( _
ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) _
Handles BackgroundWorker1.RunWorkerCompleted
If (e.Error IsNot Nothing) Then
MessageBox.Show(e.Error.Message)
Else
lblCount.Text = e.Result.ToString()
End If
End Sub

 

This code uses the RunWorkerCompleted event handler of the BackgroundWorker1 component. The RunWorkerCompleted event handler is executed when an asynchronous operation completes successfully or terminates due to error. This code then verifies the e.Error property to ensure that no error occurred during the asynchronous operation. If an error has occurred during the asynchronous operation, this code displays the error message using the e.Error.Message property. If there was no error, this code sets the Text property of the Label control lblCount to the value returned by the asynchronously executed method FetchMonthData. The return value of the FetchMonthData is stored in the Result property of the RunWorkerCompletedEventArgs object e. This code uses the ToString method to retrieve the value stored in the Result property as a string.

 

You should not use the code fragments that use the BackgroundWorker1_ProgressChanged event handler, because this event handler is executed when the ReportProgress method is called. The BackgroundWorker1_ProgressChanged event handler is commonly used to set the value of the ProgressBar control that is used to display asynchronous method execution progress.

 

You should not use the code fragments that specify Result as a property of the BackgroundWorker1 object. These code fragments are incorrect because Result is a property of the RunWorkerCompletedEventArgs object e. If used, these code fragments will generate compile-time errors.

 

Reference:
MSDN Library > Development Tools and Languages > Visual Studio > Windows-based Applications, Components, and Services > Creating Windows-based Applications > Windows Forms > Getting Started with Windows Forms > Windows Forms Controls > Controls to Use on Windows Forms > BackgroundWorker Component > BackgroundWorker

Like what you see? Share it.Share on Google+Share on LinkedInShare on FacebookShare on RedditTweet about this on TwitterEmail this to someone
cmadmin

ABOUT THE AUTHOR

Posted in Archive|

Comment: