无法使用WebClient进行多重下载

时间:2015-10-26 15:56:32

标签: vb.net visual-studio webclient

我之前已经问过这个问题,但我有一种特殊的方式我想通过webclient下载文件,我想通过Do While语句一次下载每个文件,但它只是添加了要下载的文件并转到下一个同时下载另一个文件的任务,这样它就会相互抵消并使应用程序崩溃。

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

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

所以基本上在Do While语句中它开始下载然后继续而不等待下载完成然后立即下载,然后崩溃程序/干扰显示下载名称的标签,原因这是一个与其他人不同的问题是我需要它来指定下载名称,因为我可以通过将下载添加到列表中来完成其他教程......

所以,如果有人可以帮我做Do While语句等待下载完成,那么继续下一个而不丢失文件名和类似的东西,请给我一些提示,谢谢!

我知道DownloadFile等待下载完成然后继续,但我需要它来显示下载进度和下载字节和类似的东西......

P.S。很抱歉,如果这很难解释,很难解释,谢谢。

1 个答案:

答案 0 :(得分:0)

通过添加await wc.DownloadFileTaskAsync(...),它完美无缺,非常感谢“GSerg”,谢谢!