我不确定如何说出我的问题。我试图在foreach循环中下载500个csv文件,我正在下载的东西出错。所以我在每个webClient.DownloadFile调用之前和之后放了一个.text输出。问题是我似乎没有输出每个.text文件;如果它成功,它将在结束时全部输出,如果失败则不输出任何。我不知道有一些线索/小修道具。谁能帮助我做我想做的事情?谢谢(这方面的菜鸟)。以下是我的代码。
foreach (var ticker in tickers)
{
tbOutput.Text += "Starting Download of : " + ticker + "\n";
var url = string.Format(urlPrototype, ticker, startMonth, startDay, startYear, finishMonth, finishDay, finishYear, "d");
var csvfile = directory + "\\" + ticker.ToUpper() + ".csv";
webClient.DownloadFile(url, csvfile);
tbOutput.Text += "End Download of : " + ticker + "\n";
}
答案 0 :(得分:0)
很难说你演唱的是什么技术,但是你最有可能会调用一个事件或者回复,所以你在完全执行后得到结果,如果你想看到你下载的每个文件的进度你可以尝试使用像
这样的想法答案 1 :(得分:0)
尝试使用DownloadFile,因为它会抛出WebException。 https://msdn.microsoft.com/en-us/library/ez801hhe%28v=vs.100%29.aspx
foreach (var ticker in tickers)
{
tbOutput.Text += "Starting Download of : " + ticker + "\n";
var url = string.Format(urlPrototype, ticker, startMonth, startDay, startYear, finishMonth, finishDay, finishYear, "d");
var csvfile = directory + "\\" + ticker.ToUpper() + ".csv";
ThreadPool.QueueUserWorkItem((o) =>
{
try
{
webClient.DownloadFile(url, csvfile);
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
{
//do something or output like
tbOutput.Text += "End Download of : " + ticker + " (OK)\n";
}));
}
catch (Exception ex)
{
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
{
//do something or output like
tbOutput.Text += "End Download of : " + ticker + " (FAIL)\n";
}));
}
finally
{
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
{
//do something or output like
//tbOutput.Text += "End Download of : " + ticker + "\n";
}));
}
}
}