相关疑难解决方法(0)

如何使用webclient异步下载多个文件,但一次只能一个?

很难找到使用webclient类异步方法下载多个文件的代码示例,但一次下载一个.

如何启动异步下载,但要等到第一个完成,直到第二个,等等.基本上是一个问题.

(注意我不想使用同步方法,因为异步方法的功能增加了.)

以下代码立即启动我的所有下载.(进度条到处都是)

private void downloadFile(string url)
        {
            WebClient client = new WebClient();

            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
            client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);

            // Starts the download
            btnGetDownload.Text = "Downloading...";
            btnGetDownload.Enabled = false;
            progressBar1.Visible = true;
            lblFileName.Text = url;
            lblFileName.Visible = true;
            string FileName = url.Substring(url.LastIndexOf("/") + 1,
                            (url.Length - url.LastIndexOf("/") - 1));
             client.DownloadFileAsync(new Uri(url), "C:\\Test4\\" + FileName);

        }

        void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {

        }

        void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            double bytesIn = double.Parse(e.BytesReceived.ToString());
            double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
            double …
Run Code Online (Sandbox Code Playgroud)

c# asynchronous webclient

24
推荐指数
1
解决办法
4万
查看次数

标签 统计

asynchronous ×1

c# ×1

webclient ×1