How to report in webclient progresschanged the number of downloaded files ?

sharon glipman 441 Reputation points
2021-10-16T14:16:08.937+00:00

The main goal is know in the completed event or other method what type of files have been downloaded and finished downloaded for example all text files or all radImage files and once one of the group of this files have been downloaded do something with the files.

The way it is now it's hard to tell what files have finished downloaded. For example how do I know in the completed event that all the radImage files or satImage files have been downloaded ? Not one or two all the files of each group. If the radImage files downloaded all the files already do something with them when the satImage files will finish download do something with them too.

private async void BtnDownloadFile_Click(object sender, RoutedEventArgs e)
        {
            btnDownloadFile.IsEnabled = false; // Disable to prevent multiple runs

            using (System.Net.WebClient wc = new System.Net.WebClient())
            {
                wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36");

                wc.DownloadProgressChanged += OnDownloadProgressChange;
                wc.DownloadFileCompleted += OnDownloadFileComplete;

                stopwatch.Start();
                await DownloadAsync(wc);
            }
        }

        private void OnDownloadProgressChange(object sender, System.Net.DownloadProgressChangedEventArgs e)
        {
            lblMumberOfFiles.Content = urls.Count.ToString();

            // Calculate progress values
            string downloadProgress = e.ProgressPercentage + "%";
            string downloadSpeed = string.Format("{0} MB/s", (e.BytesReceived / 1024.0 / 1024.0 / stopwatch.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

            // Set values to contols
            lblProgress.Content = progress;
            progBar.Value = e.ProgressPercentage;
        }

        private async void OnDownloadFileComplete(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            lblProgress.Content = "Download complete!";
            stopwatch.Reset();
            btnDownloadFile.IsEnabled = true;

            if(e.Error == null)
            {

            }

            if (extractOnce == false)
            {
                r.GetRadarImages();
                await Task.Delay(5000);

                for (int i = 0; i < r.links.Count; i++)
                {
                    urls.Add(r.links[i]);
                }

                ExtractLinks();
                extractOnce = true;
            }
        }

And the downloading method DownloadAsync code :

int countRad = 0;
        int countSat = 0;
        private async Task DownloadAsync(WebClient Client)
        {
            for (int i = 0; i < urls.Count; i++)
            {
                stopwatch.Start();

                string extractFile = textBoxSatelliteFolder.Text + "\\extractFile.txt";
                string fNameRad = textBoxRadarFolder.Text + "\\radImage" + countRad + ".gif";
                string fNameSat = textBoxSatelliteFolder.Text + "\\satImage" + countSat + ".gif";
                if (urls[i] == "https://mys.com/")
                {
                    await Client.DownloadFileTaskAsync(new Uri(urls[i]), extractFile);
                }
                else
                {
                    if (urls[i].Contains("Radar"))
                    {
                        await Client.DownloadFileTaskAsync(new Uri(urls[i]), fNameRad);

                        countRad++;
                    }
                    else
                    { 
                        await Client.DownloadFileTaskAsync(new Uri(urls[i]), fNameSat);

                        countSat++;
                    }
                }
            }
        }

The problems are first I'm downloading first time a file and save it as text file and build links from it and then downloading the links from it.

So in total I have for example 34 files to download. I download it in two times first the text file then starting the rest of the links downloads.
So I want to report the number of files that have to be download for example total 34 then to display a counter 34-33-32-31...
And to make like 3 labels each for each category :

label1 for text files for exmaple Text Files To Download = 1 and then when the text file downloaded without errors change the counter of the text files to 0.

label2 for radImage files type gif for example Radar Images File To Download = 21 and count down when downloading this files

Same a label for satImage files.

The radImage files and satImage files I download in the DownloadAsync method.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,671 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,243 questions
{count} votes