使用WebClient下载文件时,我遇到异常,不支持并发I / O操作,如何解决?



按钮点击事件开始下载

private void btnStart_Click(object sender, EventArgs e)
        {
            downloadFile(links);           
        }

链接是一个列表,其中包含一些HTTP链接。

和WebClient Events

private void downloadFile(IEnumerable<string> urls)
        {
            foreach (var url in urls)
            {
                _downloadUrls.Enqueue(url);
            }
            // Starts the download
            btnStart.Text = "Downloading...";
            btnStart.Enabled = false;
            pBarFileProgress.Visible = true;
            DownloadFile();
            label2.Visible = true;
            label3.Visible = true;
            label4.Visible = true;
            label7.Visible = true;
            label3.Text = "";
            label7.Text = "";
            label2.Text = "";
            label4.Text = "";
        }
        private void DownloadFile()
        {
            if (_downloadUrls.Any())
            {
                client.DownloadProgressChanged += client_DownloadProgressChanged;
                client.DownloadFileCompleted += client_DownloadFileCompleted;
                url = _downloadUrls.Dequeue();
                if (url.Contains("animated") && url.Contains("infra"))
                {
                    string startTag = "animated/";
                    string endTag = "/infra";
                    int index = url.IndexOf(startTag);
                    int index1 = url.IndexOf(endTag);
                    fname = url.Substring(index + 9, index1 - index - 9);
                    var countryName = codeToFullNameMap[fname];
                    downloadDirectory = tbxMainDownloadPath.Text;
                    downloadDirectory = Path.Combine(downloadDirectory, countryName);
                }
                else
                {
                    fname = "Tempfile";
                    downloadDirectory = tbxMainDownloadPath.Text;
                }
                client.DownloadFileAsync(new Uri(url), downloadDirectory + "\" + fname + ".gif", url);
                lastDownloadedFile = downloadDirectory + "\" + fname + ".gif";
                return;
            }
            // End of the download
            label2.Text = "All files have been downloaded";
        }
        private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                tracker.NewFile();
                DownloadFile();
                return;
                // handle cancelled scenario
            }
            if (e.Error != null)
            {
                // handle error scenario
                throw e.Error;
            }
            label2.Text = "Download Complete";
            string lastUrl = (string)e.UserState;
            listView1.BeginUpdate();
            foreach (ListViewItem li in listView1.Items)
            {
                if (li.SubItems[2].Text == lastUrl)
                {
                    li.SubItems[0].Text = "Downloaded";
                    li.SubItems.Add("Color");
                    li.SubItems[0].ForeColor = Color.Green;
                    li.UseItemStyleForSubItems = false;
                }
            }
            listView1.EndUpdate();
            tracker.NewFile();
            DownloadFile();
        }
        void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            tracker.SetProgress(e.BytesReceived, e.TotalBytesToReceive);
            pBarFileProgress.Value = (int)(tracker.GetProgress() * 100.0);
            label3.Text = e.BytesReceived + "/" + e.TotalBytesToReceive;
            label7.Text = tracker.GetBytesPerSecondString();
            label2.Text = "Downloading";
            label4.Text = downloadDirectory + "\" + fname + ".gif";
        }

例外是在线编号178

client.DownloadFileAsync(new Uri(url), downloadDirectory + "\" + fname + ".gif", url);

和第233行是在完成的事件中:

DownloadFile();

我尝试在许多答案中使用Google进行Google,它说下载尚未在运行新的WebClient请求之前完成。我确定是否正在完成完成的事件:"当前下载均未完成?

我应该如何解决和处理此例外?

例外:

blockquote Innerexception: Hresult = -2146233067 消息= WebClient不支持并发I/O操作。 源=系统 堆栈跟踪: at System.net.webclient.clearwebclientstate() 在System.net.webclient.downloadfileasync(URI地址,字符串文件名,Object UserToken) 在form1.cs中的下载multiplefiles.form1.downloadfile():第178行 at downloadmultiplefiles.form1.client_downloadfilecompleted(对象发送者,asynccompletedeventargs e)e form1.cs:第233行 在system.com.ponentmodel.asynccompletedeventhandler.invoke(对象发送者,asynccompletedeventargs e) 在system.net.webclient.ondownloadfilecompleted(asynccompletedeventargs e) 在system.net.webclient.downloadfileoperationcompleted(object arg) Innerexception:

调用

的等待方法
client.DownloadFileAsync(new Uri(url), downloadDirectory + "\" + fname + ".gif", url).Wait();

尝试

最新更新