How to sum the last reported progress percentages of all downloads and divide it by the number of all downloads to get the total progress percentage across all downloads using webclient ?

Chocolade 516 Reputation points
2022-08-30T11:46:31.71+00:00
public void DownloadFile(string urlAddress, string location)  
        {  
            Stopwatch sw = new Stopwatch();  
            using (WebClient webClient = new WebClient())  
            {  
                webClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0 Chrome");  
                webClient.DownloadFileCompleted += new AsyncCompletedEventHandler((sender, e) => Completed(sender, e, sw));  
                webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler((sender, e) => ProgressChanged(sender, e, sw));  
                Uri URL = new Uri(urlAddress);  
                sw.Start();  
                try  
                {  
                    webClient.DownloadFileAsync(URL, location);  
                }  
                catch (Exception ex)  
                {  
                    MessageBox.Show(ex.Message);  
                }  
            }  
        }  
  
        private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e, Stopwatch sw)  
        {  
            string downloadProgress = e.ProgressPercentage + "%";  
            string downloadSpeed = string.Format("{0} MB/s", (e.BytesReceived / 1024.0 / 1024.0 / sw.Elapsed.TotalSeconds).ToString("0.00"));  
            string downloadedMBs = Math.Round(e.BytesReceived / 1024.0 / 1024.0) + " MB";  
            string totalMBs = Math.Round(e.TotalBytesToReceive / 1024.0 / 1024.0) + " MB";  
  
            // Format progress string  
            string progress = $"{downloadedMBs}/{totalMBs} ({downloadProgress}) @ {downloadSpeed}"; // 10 MB / 100 MB (10%) @ 1.23 MB/s  
  
            progressBarText1.Value = e.ProgressPercentage;  
            progressBarText1.Refresh();  
            progressBarText1.CustomText = progress;  
        }  
  
        private void Completed(object sender, AsyncCompletedEventArgs e, Stopwatch sw)  
        {  
            if (e.Cancelled == true)  
            {  
                btnStartDownload.Text = "Fails.";  
                sw.Stop();  
            }  
            else  
            {  
                btnStartDownload.Text = "Finish.";  
                sw.Stop();  
  
                if (videosLinks.Count > 0)  
                {  
                    videosLinks.RemoveAt(0);  
                    string fileName = System.IO.Path.GetFileName(videosLinks[0]);  
                    DownloadFile(videosLinks[0], @"D:\Videos\videos\" + fileName);  
                }  
            }  
        }  
  
        static readonly string[] SizeSuffixes =  
                  { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };  
        static string SizeSuffix(Int64 value, Int64 totalValue)  
        {  
            if (value < 0) { return "-" + SizeSuffix(-value, totalValue); }  
  
            int i = 0;  
            decimal dValue = (decimal)value;  
            while (Math.Round(dValue / 1024) >= 1)  
            {  
                dValue /= 1024;  
                i++;  
            }  
  
            return string.Format("{0:n1} {1} {2}", dValue, SizeSuffixes[i], (totalValue / 1024d / 1024d).ToString("0.00"));  
        }  

this is working fine but it's reporting the progress download per file.

how can i make that it will also report progress of the overall downloads ? i added another progressbar name progressBar2 and to this i want to report the overall download process and not per file like i do to progressBar1.

not sure how to make the calculation and where to do it and reporting to the progressBarText2.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,820 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,194 questions
{count} votes