VB - 使用DownloadFileASync(WebClient)进行多次下载

时间:2011-10-28 15:19:53

标签: vb.net webclient multiple-files downloadfileasync

我正在尝试根据用户在表单上选择的内容下载多个文件。 我有多个复选框,所以如果用户选择Checkboxes 1,3,4,我希望webclient下载文件1.txt,3.txt,4.txt。 WebClient方法导致“WebClient不支持并发I / O操作”。错误。

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

我有一个跟踪下载的进度条,以及一个调用消息框的已完成事件。

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

我怎样才能对此进行编程以下载所有已检查的文件? 我不介意用户是否只看到显示已下载总文件的进度条或剩余时间,但我确实需要在完成所有下载后显示一些内容。

任何建议?

1 个答案:

答案 0 :(得分:2)

我认为是因为您使用单个WebClient实例同时执行多个HTTP请求。尝试使用多个实例。