VB-使用DownloadFileASync(WebClient)进行多次下载 [英] VB - Using DownloadFileASync (WebClient) for multiple downloads

查看:225
本文介绍了VB-使用DownloadFileASync(WebClient)进行多次下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

有什么建议吗?

推荐答案

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

I think is because of you using single WebClient instance to execute several HTTP requests at the same time. Try to use several instances.

这篇关于VB-使用DownloadFileASync(WebClient)进行多次下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆