DownloadFileAsync无法正常工作/调用。即使没有下载文件,也会跳过DownloadCompleted

时间:2014-05-01 01:19:03

标签: vb.net webclient basic downloadfileasync

我有问题。我正在使用Visual Studio 2013(Visual Basic),并且在我的计算机上一切正常,但在其他方面却没有。

示例:即使没有下载的文件,也不会调用client1.DownloadFileAsync并调用client1_DownloadCompleted。为什么只是跳过它??

代码(在DownloadButton_Click上):`

            AddHandler client1.DownloadProgressChanged, AddressOf client_ProgressChanged
            AddHandler client1.DownloadFileCompleted, AddressOf client1_DownloadCompleted
            AddHandler client2.DownloadProgressChanged, AddressOf client_ProgressChanged
            AddHandler client2.DownloadFileCompleted, AddressOf client2_DownloadCompleted
            AddHandler client3.DownloadProgressChanged, AddressOf client_ProgressChanged
            AddHandler client3.DownloadFileCompleted, AddressOf client3_DownloadCompleted
            AddHandler client4.DownloadProgressChanged, AddressOf client_ProgressChanged
            AddHandler client4.DownloadFileCompleted, AddressOf client1_DownloadCompleted
            AddHandler client5.DownloadProgressChanged, AddressOf client_ProgressChanged
            AddHandler client5.DownloadFileCompleted, AddressOf client2_DownloadCompleted
            AddHandler client6.DownloadProgressChanged, AddressOf client_ProgressChanged
            AddHandler client6.DownloadFileCompleted, AddressOf client3_DownloadCompleted


            If CheckBox7.Checked Then
                odkoliko = odkoliko + 1
                Label8.Text = "Popravlja se " & koliko & "/" & odkoliko & " stavka!"
                client5.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/s/7wq47s49aw5qrps/Obrisi%20PMF%20Novi%20Sad.lnk"), filePath5)
            End If
            If CheckBox4.Checked Then
                odkoliko = odkoliko + 1
                Label8.Text = "Popravlja se " & koliko & "/" & odkoliko & " stavka!"
                client4.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/s/69q4pd281ka3lmf/Repair.exe"), filePath4)
            End If
            If CheckBox2.Checked Then
                odkoliko = odkoliko + 1
                Label8.Text = "Popravlja se " & koliko & "/" & odkoliko & " stavka!"
                client1.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/s/u4iy3katgl394wf/PMF%20Novi%20Sad%20UPDATE.exe"), filePath1)
            End If
            If CheckBox5.Checked Then
                odkoliko = odkoliko + 1
                Label8.Text = "Popravlja se " & koliko & "/" & odkoliko & " stavka!"
                client2.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/s/tuy0zspy1i1y84u/chatroom.html"), filePath2)
            End If
            If CheckBox3.Checked Then
                odkoliko = odkoliko + 1
                Label8.Text = "Popravlja se " & koliko & "/" & odkoliko & " stavka!"
                client6.DownloadFileAsync(New Uri("https://dl.dropboxusercontent.com/s/1n7oe7d1ms72qo3/Update.exe"), filePath6)
            End If
    End Select
End Sub

Private Sub client_ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)

End Sub

Private Sub client1_DownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
    koliko = koliko + 1
    Label8.Text = "Popravlja se " & koliko & "/" & odkoliko & " stavka!"
    AllDownloadFinished()
End Sub

Private Sub client2_DownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
    koliko = koliko + 1
    Label8.Text = "Popravlja se " & koliko & "/" & odkoliko & " stavka!"
    AllDownloadFinished()
End Sub

Private Sub client3_DownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
    koliko = koliko + 1
    Label8.Text = "Popravlja se " & koliko & "/" & odkoliko & " stavka!"
    AllDownloadFinished()
End Sub

Private Sub client4_DownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
    koliko = koliko + 1
    Label8.Text = "Popravlja se " & koliko & "/" & odkoliko & " stavka!"
    AllDownloadFinished()
End Sub

Private Sub client5_DownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
    koliko = koliko + 1
    Label8.Text = "Popravlja se " & koliko & "/" & odkoliko & " stavka!"
    AllDownloadFinished()
End Sub

Private Sub client6_DownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
    koliko = koliko + 1
    Label8.Text = "Popravlja se " & koliko & "/" & odkoliko & " stavka!"
    AllDownloadFinished()
End Sub

Public Sub AllDownloadFinished()
    If koliko > odkoliko Then
        Label6.Text = "GOTOVO!"
        ProgressBar1.Style = ProgressBarStyle.Continuous
        ProgressBar1.MarqueeAnimationSpeed = 0
        Panel1.Visible = False
        Panel2.Visible = False
        Panel3.Visible = True
    End If
End Sub`

正如我在电脑上说的那样,代码完美无缺,但我的朋友却没有。也许问题不在代码中,而不是.NET Framwork,Visual c + + Redistributable,Web Deployment或其他一些需要安装的Microsoft应用程序!

1 个答案:

答案 0 :(得分:0)

您的代码没有检查异步错误。一旦您看到错误,您就可以进行故障排除。

试试这个:

Private Sub client1_DownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
    If e.Error IsNot Nothing Then
        Msgbox(e.Error.Message)
        Exit Sub
    ElseIf e.Cancelled Then
        Msgbox("Cancelled")
        Exit Sub
    End If
    koliko = koliko + 1
    Label8.Text = "Popravlja se " & koliko & "/" & odkoliko & " stavka!"
    AllDownloadFinished()
End Sub

更多信息: http://msdn.microsoft.com/en-us/library/system.componentmodel.asynccompletedeventargs(v=vs.110).aspx