问题
I'm trying to download multiple files based on what a user has selected on a form. I have multiple checkboxes in place, so If a user would select Checkboxes 1,3,4 I would want the webclient to download files 1.txt, 3.txt, 4.txt. The WebClient method is causing a "WebClient does not support concurrent I/O operations." error.
If chk1.Checked Then
WC.DownloadFileAsync(New Uri("http://www.google.com/1.txt), Path.Combine(DataSource & strDirectory, "1.txt"))
End If
If chk2.Checked Then
WC.DownloadFileAsync(New Uri("http://www.google.com/2.txt), Path.Combine(DataSource & strDirectory, "2.txt"))
End If
If chk3.Checked Then
WC.DownloadFileAsync(New Uri("http://www.google.com/3.txt), Path.Combine(DataSource & strDirectory, "3.txt"))
End If
If chk4.Checked Then
WC.DownloadFileAsync(New Uri("http://www.google.com/4.txt), Path.Combine(DataSource & strDirectory, "4.txt"))
End If
I do have a progress bar that tracks the download, as well as a completed event that calls a messagebox.
Private Sub WC_DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles WC.DownloadProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub WC_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles WC.DownloadFileCompleted
MessageBox.Show("Download complete", "Download", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
How could I go about programming this to download all files checked? I don't mind if the user only sees that progress bar showing total files downloaded or time remaining, but I do need something to show when all downloads are completed.
Any Suggestions?
回答1:
I think is because of you using single WebClient instance to execute several HTTP requests at the same time. Try to use several instances.
来源:https://stackoverflow.com/questions/7931282/vb-using-downloadfileasync-webclient-for-multiple-downloads