如果你不打扰阅读整个文本,可以跳到最后两个点:对C#与队列列表异步下载
这个网站已经帮助我的时候一打已经过去,但现在我真的需要帮助。
的问题如下:
首先,我用show UI选项一起使用的DownloadFile功能。这很好,但用户界面很丑,并没有太多可用的选项。
然后,我切换到DownloadFileAsync与改变的进度事件,以基本上有我自己的用户界面。我遇到的唯一问题是我循环访问程序必须下载的文件列表,并调用下载函数(调用DownloadAsync函数)。就像这样:
foreach (ListViewItem t in themeList.CheckedItems) { DownloadFile(file to be downloaded); }
但显然这并没有工作,因为DownloadFileAsync功能不支持在同一时间多个呼叫,因为没有排队系统,如DownloadFile了,所以它会只下载第一称为文件。 因此,我所做的是创建一个函数,将要下载的文件添加到数组中,并通过列表使背景工作循环,然后等待调用DownloadAsync,直到完成上一个下载为止。这有点解决了。下面是代码:
#region "Download functions" //Function that converts download speed to a nice user friendly format private static string BpsToString(double bps) { var m = new string[] { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; var i = 0; while (bps >= 0.9 * 1024) { bps /= 1024; i++; } return String.Format("{0:0.00} {1}/sec", bps, m[i]); } private bool _complete = false; private string _speed; private int _secondsRemaining = -1; private long _transferred = 0; private Stopwatch _sw = new Stopwatch(); private List<string[]> _fd = new List<string[]>(); private void DownloadFile(string url, string des, bool overwrite = false) { if (overwrite) //if the file needs to be overwritten or not { if (File.Exists(des)) File.Delete(des); } else { if (File.Exists(des)) return; } if (!Directory.Exists(Path.GetDirectoryName(des))) //create the directory if it doesn't exist Directory.CreateDirectory(Path.GetDirectoryName(des)); string[] file = {url, des}; _fd.Add(file); //add file to queue list if(!backgroundDownloader.IsBusy) //if downloader isn't doing anything, start it again backgroundDownloader.RunWorkerAsync(); } //function called by the backgroundworker to actually download the file private void ContinueDownloadFile(string url, string des) { var webClient = new WebClient(); webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); webClient.DownloadFileAsync(new Uri(_fd[0][0]), _fd[0][1]); } //when download completed, set progress bar to 0% and remove the first (0) download from the queue private void Completed(object sender, AsyncCompletedEventArgs e) { SetProgressText("Idle"); SetProgressValue(0); if(_fd.Count != 0) _fd.RemoveAt(0); _complete = true; //if it's complete, set to true so the backgroundworker knows it can start the next download } //progress bar value change and status change for download speed etc... private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) { if(progressLabel.Text == "Idle") SetProgressText("Downloading..."); if (_sw.Elapsed >= TimeSpan.FromSeconds(1)) { _sw.Stop(); var bytes = e.BytesReceived - _transferred; var bps = bytes * 1000.0/_sw.Elapsed.TotalMilliseconds; _speed = BpsToString(bps); _secondsRemaining = (int)((e.TotalBytesToReceive - e.BytesReceived)/bps); _transferred = e.BytesReceived; _sw.Reset(); _sw.Start(); SetProgressText("Downloading: " + e.ProgressPercentage + "% | Seconds remaining: " + _secondsRemaining + " | Files remaining: " + _fd.Count + " | Speed: " + _speed); } SetProgressValue(e.ProgressPercentage); } //the backgroundworker who starts the downloads from the list one by one private void BackgroundDownloaderDoWork(object sender, DoWorkEventArgs e) { while (_fd.Count != 0) { _sw.Start(); _complete = false; //let the backgroundworker wait till the download is complete ContinueDownloadFile(_fd[0][0], _fd[0][1]); while(!_complete) //let it wait here Thread.Sleep(100); _sw.Stop(); _sw.Reset(); } } #endregion
所以basicly我的下一个问题是,程序必须等待与执行任何代码,直至下载完成。我做这个做:
while (_fd.Count != 0) Application.DoEvents();
这显然不是最好的解决办法,因为他们可以通过点击其他的事情,而下载是忙碌的,但是,是的Thread.Sleep也只是冻结一切。 相反,我会在主窗体顶部重点放置一个等待窗体(也许这里是一个进度条,而不是主窗体),所以它们不能单击主窗体并将Thread.Sleep放在主窗体上形成?
你会如何解决这个问题?你是否还会使用循环遍历文件数组的背景工作,或者是否有更简单,更高效的方法。也许不使用DownloadFileAsync,但手动套接字下载?
我basicly想要的是同步下载文件,但有自己的UI(所以我需要使用异步下载功能)。哈哈
我希望我通知你足够。提前致谢。
也许这是“TL; DR”的信息? –
我会让它更短,因为它确实包含一些不必要的东西 – d0ggy
你对子弹#5的计划听起来是最好的方式。您也可以尝试使用任务并行库(TPL)。它可以简化并发下载调用,并且可以使用同步文件下载器。 –