Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用WebClient下载文件时,i';m getting exception不支持并发I/O操作如何解决它?_C#_.net_Winforms - Fatal编程技术网

C# 使用WebClient下载文件时,i';m getting exception不支持并发I/O操作如何解决它?

C# 使用WebClient下载文件时,i';m getting exception不支持并发I/O操作如何解决它?,c#,.net,winforms,C#,.net,Winforms,启动下载的按钮单击事件 private void btnStart_Click(object sender, EventArgs e) { downloadFile(links); } links是一个包含一些http链接的列表 以及网络客户端事件 private void downloadFile(IEnumerable<string> urls) { forea

启动下载的按钮单击事件

private void btnStart_Click(object sender, EventArgs e)
        {
            downloadFile(links);           
        }
links是一个包含一些http链接的列表

以及网络客户端事件

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";
        }
第233行为已完成事件:

DownloadFile();
我试着用谷歌搜索了很多答案,其中说在运行一个新的webclient请求之前下载还没有完成。我确信如果它已经完成了,那就意味着当前的下载已经完成了没有

我应该如何解决和处理此异常

例外情况:

大宗报价 内部异常: HResult=-2146233067 Message=WebClient不支持并发I/O操作。 来源=系统 堆栈跟踪: 在System.Net.WebClient.ClearWebClient状态()中 位于System.Net.WebClient.DownloadFileAsync(Uri地址、字符串文件名、对象userToken) 在Form1.cs中的DownloadMultipleFiles.Form1.DownloadFile()处:第178行 在下载multiplefiles.Form1.client时,下载文件已完成(对象发送方,AsyncCompletedEventArgs e),在Form1.cs中:第233行 位于System.ComponentModel.AsyncCompletedEventHandler.Invoke(对象发送方,AsyncCompletedEventArgs e) 在System.Net.WebClient.OnDownloadFileCompleted(AsyncCompletedEventArgs e)中 在System.Net.WebClient.DownloadFileOperationCompleted(对象参数)处 内部异常:


调用的等待方法

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

试一试

这是公认的答案吗?这与下载文件基本相同,您正在失去异步功能,因为它会阻塞线程。如unser所述,您是对的。无论如何,这是解决他的问题的一种方法。另一种选择是添加一个whil(true)循环,或者正确地等待填充。这根本不是解决问题的正确方法。如果下载量很大,它将阻塞线程很长时间,直到整个下载完成。随着while循环内下载完成,它将是一个快速块,直到!webclient.Isbusy。等待任务也会有同样的问题,因为即使下载完成,webclient可能仍然很忙,因此op会问这个问题。或的可能重复
client.DownloadFileAsync(new Uri(url), downloadDirectory + "\\" + fname + ".gif", url).Wait();