本文介绍了需要使用webclient报告进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 应用程序异步下载多个文件,所以当我输入10个链接到列表框10个文件开始下载。我想为每个文件动态创建进度条并报告进度。这就是我用来报告进度的内容The app downloading multiple files asynchronously, so when i enter 10 links into listbox 10 files start to download. I wast to create progress bars dynamically for every file and report progress. This is what im using to report progressclient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(worker_ProgressChanged);但它无效。 当我做 but its not working.When i doworker.ReportProgress(10); 它有效,但我只能报告一个整数,而不是client.DownloadProgressChanged。 我的问题是如何将client.DownloadProgressChanged附加到我的worker_ProgressChanged类。 我尝试过: private void backgroundWorker1_DoWork(object sender,DoWorkEventArgs e) { List< string> myList =(List< string>)e.Argument; foreach(myList中的字符串yo) { ProgressBar bar = new ProgressBar(); 位置+ = 30; bar.Width = 300; bar.Location = new Point(300,position); Invoke(new ToDoDelegate(()=> bar.Location = new Point(150, position))); Invoke(new ToDoDelegate(()=> this.Controls.Add(bar))); Invoke(new ToDoDelegate(()= > bar.Visible = true)); WebClient客户端=新WebClient(); string FileName = yo.Substring(yo.LastIndexOf(/)+ 1,(yo.Length - yo.LastIndexOf(/) - 1)); client.DownloadFileAsync(new Uri(yo),C:\\Test4 \\+ FileName); client.DownloadProgressChanged + = new DownloadProgressChangedEventHandler(worker_ProgressChanged); //(发件人为BackgroundWorker).ReportProgress((client.DownloadFileAsync(new Uri(yo),FileName))); worker.ReportProgress(10); } } private void worker_ProgressChanged(object sender,ProgressChangedEventArgs e) { MessageBox.Show(我正在报告); }it works but im can only report an integer and not client.DownloadProgressChanged.My question is how do i attach client.DownloadProgressChanged to my worker_ProgressChanged class.What I have tried:private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { List<string> myList = (List<string>)e.Argument; foreach(string yo in myList) { ProgressBar bar = new ProgressBar(); position += 30; bar.Width = 300; bar.Location = new Point(300, position); Invoke(new ToDoDelegate(() => bar.Location = new Point(150, position))); Invoke(new ToDoDelegate(() => this.Controls.Add(bar))); Invoke(new ToDoDelegate(() => bar.Visible = true)); WebClient client = new WebClient(); string FileName = yo.Substring(yo.LastIndexOf("/") + 1, (yo.Length - yo.LastIndexOf("/") - 1)); client.DownloadFileAsync(new Uri(yo), "C:\\Test4\\" + FileName); client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(worker_ProgressChanged); // (sender as BackgroundWorker).ReportProgress((client.DownloadFileAsync( new Uri(yo), FileName))); worker.ReportProgress(10); } } private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) { MessageBox.Show("I am reporting"); }推荐答案以下代码将对您有所帮助:The following code will help you:WebClient webClient = new WebClient();webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompleted);webClient.QueryString.Add("file", localFilePath); // To identify the filewebClient.DownloadFileAsync("URL here", localFilePath); webClient_DownloadProgressChanged 事件将如下所示:And the webClient_DownloadProgressChanged event will be like the following:void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e){ string fileProcessed = ((System.Net.WebClient)(sender)).QueryString["file"]; // Getting the local path if required var RecevedBytes = e.BytesReceived; var TotalBytes = e.TotalBytesToReceive; // use these variables if needed bar.Value = e.ProgressPercentage;} 这篇关于需要使用webclient报告进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-24 05:34