用于开始下载的按钮点击事件
private void btnStart_Click(object sender, EventArgs e)
{
downloadFile(links);
}
links是一个列表,里面有一些http链接。
WebClient事件
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();
我尝试谷歌在许多答案中说它在运行新的webclient请求之前尚未完成下载。我确定它是否正在完成事件'意味着当前下载完成没有?
我该如何解决和处理这个例外?
例外:
块引用 的InnerException: 的HResult = -2146233067 Message = WebClient不支持并发I / O操作。 来源=系统 堆栈跟踪: 在System.Net.WebClient.ClearWebClientState() 在System.Net.WebClient.DownloadFileAsync(Uri地址,String fileName,Object userToken) 在Form1.cs中的DownloadMultipleFiles.Form1.DownloadFile():第178行 在Form1.cs中的DownloadMultipleFiles.Form1.client_DownloadFileCompleted(Object sender,AsyncCompletedEventArgs e):第233行 at System.ComponentModel.AsyncCompletedEventHandler.Invoke(Object sender,AsyncCompletedEventArgs e) 在System.Net.WebClient.OnDownloadFileCompleted(AsyncCompletedEventArgs e) 在System.Net.WebClient.DownloadFileOperationCompleted(Object arg) InnerException:
答案 0 :(得分:1)
调用
的等待方法client.DownloadFileAsync(new Uri(url), downloadDirectory + "\\" + fname + ".gif", url).Wait();
试一试