[webClient.DownloadStringAsync] How to make sure download occurs in right order?

littlebigman

Well-known member
Joined
Jan 5, 2010
Messages
75
Programming Experience
Beginner
Hello

I need to write a small application to download web pages. I only need basic features, so I guess the WebClient object is good enough.

To avoid freezing the UI, I was thinking of using WebClient.DownloadStringAsync(), but dowloading must occur in the right order, while DownloadStringAsync() apparently will download pages in any order:

VB.NET:
Public Class Form1
    Private Sub AlertStringDownloaded(ByVal sender As Object, ByVal e As DownloadStringCompletedEventArgs)
        If e.Cancelled = False AndAlso e.Error Is Nothing Then
        	'Download web page, extract bit using regex, and write it to file
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim theaters As New Dictionary(Of String, String)
        theaters.Add("Theater1", "http://www.acme.com/1")
        theaters.Add("Theater2", "http://www.acme.com/2")

        'Must use async mode, or UI freezes
        For Each kvp As KeyValuePair(Of String, String) In theaters
            Dim webClient As New WebClient
            AddHandler webClient.DownloadStringCompleted, AddressOf AlertStringDownloaded

            webClient.Encoding = Encoding.UTF8

            [b]'IMPORTANT: How to make sure async download occurs in the expected order?[/b]
            webClient.DownloadStringAsync(New Uri(kvp.Value))
        Next

    End Sub
End Class

Do you know of a way to 1) download web pages in a specific order 2) in a way that won't freeze the UI?

Thank you.
 
Each time you call DownloadStringAsync it will download one String on one thread. Each one is independent so the order in which the downloads complete is independent. Does that really matter though? Presumably what matters is that you use them in the right order, so you could simply put each one into an array at the correct index as it downloads. If you use the array elements in order then the Strings downloaded will be in order.

The alternative would be to start a thread yourself and then call DownloadString in a loop on that thread. Because you're calling a synchronous method you ensure that the downloads occur in the order you want, in serial rather than in parallel. The UI still remains responsive because all the work is done on a secondary thread, although a single secondary thread rather than multiple. This will take longer though.
 
Thanks for the tips. I'll write the output into an SQLite database, so I can sort the results and write them into a single HTML file once all the threads are done.
 
Back
Top