Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27046

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

$
0
0
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:

Code:

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

            'IMPORTANT: How to make sure async download occurs in the expected order?
            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.

Viewing all articles
Browse latest Browse all 27046

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>