我有问题。我使用的是Visual Studio 2013(Visual Basic(,在我的电脑上一切正常,但在其他电脑上则不然。
示例:没有调用client1.DownloadFileAsync,即使没有下载的文件,也会调用client1_DownloadCompleted。为什么跳过它!?
代码(在下载按钮上单击(:`
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框架、Visual c++Redistributable、Web Deployment或其他需要安装的Microsoft应用程序!?
您的代码没有检查异步错误。一旦您看到错误,就可以进行故障排除。
试试这个:
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