How can I wait in form1 to the first file to be downloaded in a class before continue to the rest of the code in form1 ?

Nedudale 141 Reputation points
2021-10-29T20:30:22.983+00:00

In a class I'm downloading a file and if the file is not downloaded fine with error it will try to download it again and if it's fine it will do something else :

using (System.Net.WebClient wc = new System.Net.WebClient())
            {
                wc.DownloadFileAsync(new Uri(currentLink), folders[0] + "\\image0.gif");

                wc.DownloadFileCompleted += (s, e) =>
                {
                    if (e.Error != null)
                    {       
                            current = current.AddMinutes(-5);
                            ct = current.ToString("yyyyMMddHHmm");
                            currentLink = defaultlink + ct + ".gif";
                            wc.DownloadFileAsync(new Uri(currentLink), folders[0] + "\\image0.gif");
                    }
                    else
                    {
                        workingFirstDateTime = current;

                        GenerateRadarLinks();
                    }
                };
            }

In Form1 when I click the button to start downloading I'm calling this method :

private void btnStart_Click(object sender, EventArgs e)
        {
            dates = new List<DateTime>();
            datesonimages = new List<string>();

            btnRadarPath.Enabled = false;
            btnSatellitePath.Enabled = false;

            timeCounter1._timer.Enabled = true;
            DownloadImages();
        }

        public async void DownloadImages()
        {
            CreateDownloadFolders();

            urls = new List<string>();

            lblStatus.Text = "Downloading...";

            rad.GetRadarImages();
            await Task.Delay(15000);
            foreach (string link in rad.links)
            {
                urls.Add(link);
            }

            await sat.DownloadSatelliteAsync();
            foreach (string link in sat.SatelliteUrls())
            {
                urls.Add(link);
            }

            urlsCounter = urls.Count;

            await DownloadAsync();
        }

And in the DownloadImages method I'm using this line to wait for the class to build the links I'm waiting until the first file will be downloading fine without errors then it's building links then I'm adding the links from the class to the urls List

await Task.Delay(15000);

So I'm waiting 15 seconds. In most of the cases it's working because the first file is downloading successfully before 15 seconds but this is not a real solution not a logic solution. Who said it will take 15 seconds until the file to be downloaded fine without error ? Maybe it will try to download the file for a minute or 5 seconds.

I mean 15 seconds is enough but it's not a solution. it's more kind of workaround.

I wonder if there is a better way to wait in form1 before continue ?

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,828 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,247 questions
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,406 Reputation points
    2021-10-29T20:46:21.28+00:00

    Is there a reason why you're using DownloadFileAsync over DownloadFileTaskAsync? If you use the latter then you get the benefit of your code inside GetRadarImages flowing more sequentially & typically means it's easier to read/reason about:

    public async Task GetRadarImages() {
        using (System.Net.WebClient wc = new System.Net.WebClient()) {
            try {
                await wc.DownloadFileTaskAsync(new Uri(currentLink), folders[0] + "\\image0.gif");
    
                workingFirstDateTime = current;
    
                GenerateRadarLinks();
            } catch (Exception) {
                current = current.AddMinutes(-5);
                ct = current.ToString("yyyyMMddHHmm");
                currentLink = defaultlink + ct + ".gif";
    
                await wc.DownloadFileTaskAsync(new Uri(currentLink), folders[0] + "\\image0.gif");
            }
        }
    }
    

    The second benefit is that your DownloadImages method can just await the call to GetRadarImages because it now returns a Task:

    await rad.GetRadarImages();
    

0 additional answers

Sort by: Most helpful