1

I know this question has been asked before, but I have a special way I want to download the files through a webclient, I want to download each file at a time through a Do While statement, but it just adds the file to download and moves on to the next task that also downloads another file so it cancels each other out and crashes the application.

Private Function StartDownloads()        
        Dim wc As New WebClient
        AddHandler wc.DownloadProgressChanged, AddressOf DownloadProgressChanged
        AddHandler wc.DownloadFileCompleted, AddressOf DownloadFileCompleted
        Dim delimiterChars As Char() = {"+"c}
        Dim words As String() = RichTextBox1.Text.Split(delimiterChars)

        Dim i As Integer = 1
        Do While (i < words.Length)
            Dim delimiterChars1 As Char() = {"|"c}
            Dim words1 As String() = words(i).Split(delimiterChars1)

            Dim name As String = words1(0)
            Dim fileurl As String = words1(2)

            ToolStripStatusLabel1.Text = "Downloading " & name & ":"
            wc.DownloadFileAsync(New System.Uri(fileurl), Path.GetTempPath() & "\" & name)
            i = (i + 1)
        Loop
End Function

AND

Private Sub DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs)
        ProgressBar1.Value = e.ProgressPercentage
        ToolStripStatusLabel2.Text = String.Format("{0} MB's / {1} MB's",
        (e.BytesReceived / 1024D / 1024D).ToString("0.00"),
        (e.TotalBytesToReceive / 1024D / 1024D).ToString("0.00"))
    End Sub

So basically in the Do While statement it starts the download then continues without waiting for the download to finish and then downloads both at once, which then crashes the program/interferes with the labels displaying the download name, the reason this is a different question from others is I need it to specify the download name as I can't do with other tutorials, by adding the downloads to a list...

So if anyone can help me make the Do While statement wait for the download to finish then continue with the next without loosing the file name and stuff like that, please give me some tips, thanks!

I know that DownloadFile waits for the download to finish then continues, but I need it to show download progress and download bytes and stuff like that...

P.S. Sorry if this is confusing as it's hard to explain, thanks.

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
TymeBomb
  • 117
  • 2
  • 12
  • Try using `await wc.DownloadFileTaskAsync(...)` instead of `wc.DownloadFileAsync()`, although it's not clear from the [documentation](https://msdn.microsoft.com/en-us/library/hh193917%28v=vs.110%29.aspx) if the events will fire in this case. If they don't, you'd either have a `DoEvents` loop after the call or switch to `HttpClient` and [implement progress yourself](http://stackoverflow.com/q/20661652/11683). – GSerg Oct 26 '15 at 16:24
  • @GSerg It worked, thanks! – TymeBomb Oct 26 '15 at 17:22

1 Answers1

0

By adding await wc.DownloadFileTaskAsync(...), it worked perfectly

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
TymeBomb
  • 117
  • 2
  • 12