In this exercise, you will create a very simple application that will download the HTML content of a web page and, using regular expressions, search through that content looking for any link. Then, you will display those links in a ListBox. You will build this application in two ways. First, you will build the application using synchronous code and examine the issues with this implementation. Second, you will modify that application to use the asynchronous code so you can see how this implementation addresses the issues with the first version of the application.
Task 1 – Creating the Application in the Old Way
In this task, you will create a WPF application to download a web page and find any link in the HTML. You will write the code in the old way by using synchronous calls, and finally, run the application to see the disadvantages of this way of programming.
Open Visual Studio 11 and create a new WPF Application projectwith the name AsyncLab. Choose the language of your preference: C# or Visual Basic.
In the MainWindow.xaml designer, resize the window to 640x480.
Drop a TextBox on the left to display the HTML and a ListBox on the right to display the found links.
Drop a button at the bottom and set its content to “Start”. You will get the following XAML code:
Private Sub startButton_Click(sender As System.Object,
e As System.Windows.RoutedEventArgs) Handles startButton.Click
Dim uris As New List(Of String)()
startButton.IsEnabled = False
In between the code that disables and enables the Start button, insert a try-catch block for the main functionality of the application. Then, implement an exception handler to display a message box with the description of the error.
Dim mc As MatchCollection = Regex.Matches(result,
For Each m As Match In mc
listBox1.ItemsSource = uris
Press F5 to start debugging. When the application starts, click the Start button.
Running the application
Notice the window is unresponsive and the button is not being disabled because the UI is blocked while the application is downloading the web page.
Task 2 – Using Asynchronous Code
In this task, you will enhance the application you have started in the previous task to make it asynchronous. To do this, you will implement an asynchronous method to download the page in the background.
Add a project reference to System.Net.Http.
Make the following change at the top of the code file:
Dim response = Await New HttpClient().GetAsync("http://msdn.microsoft.com")
Dim result As String = Await response.Content.ReadAsStringAsync ()
textBox1.Text = result
When you add the await keyword, you are telling the compiler to asynchronously wait for the task returned by the method. This means that the rest of the code will be executed as a callback after the awaited method completes. You do not need to change your try-catch block in order to make this work. The exceptions that happen in the background or in the foreground will still be caught.
Press F5 to run the application and click the Start button. Notice that the application is responsive while the Start button is disabled. UI thread is never locked because the work is processed in the background.