我正在尝试使用4个网络客户端同时下载4个文件。它同时下载所有4个文件,但前两个进度条工作正常,第三个进度条与第一个进度条完全一致,第四个进度条与第二个进度条完全一致。这是我的代码与此问题相关。
Public WithEvents downloadFile1 As WebClient
Public WithEvents downloadFile2 As WebClient
Public WithEvents downloadFile3 As WebClient
Public WithEvents downloadFile4 As WebClient
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
System.Net.ServicePointManager.DefaultConnectionLimit = 10
End Sub
Private Sub startDownloadFile1()
downloadFile1 = New WebClient
Dim targetURL As String = lstURLs.Items.Item(0)
Dim destinationPath As String = "e:\Downloads\0.jpg"
downloadFile1.DownloadFileAsync(New Uri(targetURL), destinationPath)
End Sub
Private Sub startDownloadFile2()
downloadFile2 = New WebClient
Dim targetURL As String = lstURLs.Items.Item(1)
Dim destinationPath As String = "e:\Downloads\1.jpg"
downloadFile2.DownloadFileAsync(New Uri(targetURL), destinationPath)
End Sub
和similler for startDownloadFile3()和startDownloadFile()
Private Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click
startDownloadFile1()
startDownloadFile2()
startDownloadFile3()
startDownloadFile4()
End Sub
Private Sub downloadFile1_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles downloadFile1.DownloadProgressChanged
pb1.Value = e.ProgressPercentage
End Sub
Private Sub downloadFile2_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles downloadFile2.DownloadProgressChanged
pb2.Value = e.ProgressPercentage
End Sub
Private Sub downloadFile3_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles downloadFile1.DownloadProgressChanged
pb3.Value = e.ProgressPercentage
End Sub
Private Sub downloadFile4_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles downloadFile2.DownloadProgressChanged
pb4.Value = e.ProgressPercentage
End Sub
答案 0 :(得分:2)
Private Sub downloadFile3_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles **downloadFile1.**DownloadProgressChanged
pb3.Value = e.ProgressPercentage
End Sub
Private Sub downloadFile4_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles **downloadFile2.**DownloadProgressChanged
pb4.Value = e.ProgressPercentage
更改为:简单错误
Private Sub downloadFile3_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles **downloadFile3.**DownloadProgressChanged
pb3.Value = e.ProgressPercentage
End Sub
Private Sub downloadFile4_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles **downloadFile4.**DownloadProgressChanged
pb4.Value = e.ProgressPercentage