How to download a file over and over again until the fille will download successfully ?

Ben Shmil 41 Reputation points
2022-11-18T14:26:57.617+00:00
        public List<string> links = new List<string>();  
        string defaultlink;  
        DateTime current;  
        string currentLink;  
        public string workingCurrentLink;  
        public List<DateTime> dates = new List<DateTime>();  
        DateTime workingFirstDateTime;  
  
        public List<string> folders = null;  
  
        public myFiles(List<string> inputFolders)  
        {  
            folders = inputFolders;  
            current = RoundDown(DateTime.Now, TimeSpan.FromMinutes(-5));  
        }  

        public async Task PrepareLinks()  
        {  
        links = new List<string>();  
        dates = new List<DateTime>();  

        await GetFilesImages();  
        }  

        public async Task GetFilesImages()  
        {  
            defaultlink = "https://test.com/sites/default/files/ims_data/map_images/IMSRadar/IMSRadar_";  
  
            var ct = current.ToString("yyyyMMddHHmm");  
            currentLink = defaultlink + ct + ".gif";  
  
            using (System.Net.WebClient wc = new System.Net.WebClient())  
            {  
                try  
                {  
                    await wc.DownloadFileTaskAsync(new Uri(currentLink), folders[0] + "\\Image0.gif");  
                     
                    workingFirstDateTime = current;  
                    GenerateFilesLinks();  
                }  
                catch (Exception)  
                {  
                    current = current.AddMinutes(-5);  
                    ct = current.ToString("yyyyMMddHHmm");  
                    currentLink = defaultlink + ct + ".gif";  
                    await GetFilesImages();  
                }  
            }  
        }  

The way i'm doing it now by calling the same method in the exception part :

await GetFilesImages();  

make it endless loop and make the whole application freeze.

i want to try to download the file if not success then rebuild the link by minus 5 minutes and try to download the file again and so on until success. when the file has downloaded success don't dowlnoad again. and call the method in the try part GenerateFilesLinks();

i'm calling this class from form1 :

private async void btnStart_Click(object sender, EventArgs e)  
        {  
            CreateDownloadFolders createDownloadFolders = new CreateDownloadFolders();  
            createDownloadFolders.GenerateDownloadFolders(textBoxPath.Text);  
  
            lblStatus.Text = "Preparing Downloads";  
            loadingLabel1.Visible = true;  
            await myClass.PrepareLinks();  
         }  
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,837 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,301 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 48,821 Reputation points
    2022-11-18T16:50:33.567+00:00

    My personal recommendation is to move to HttpClient away from WebClient. Then add the Polly library with the retry policy you want. Then your code can focus on just starting the download and waiting for it to complete (or to fail because the retry policy gives up). Focus on your code and let the libraries handle what they are good at.

    Also because HttpClient is async only you don't have to block your UI. You start the download in the background, optionally disabling some UI features, and then wait for it to complete. When it is done then you update your UI again. There are many articles on how to use async with winforms so here is just one article to show you how to do it.

    Finally be aware that you should not bother retrying in all cases. There are some errors that will likely never resolve so blindly trying again is not the best solution. This is something that Polly does automatically and is yet another reason you should rely on a third party library for this common functionality.

    0 comments No comments