Why it's taking 17-18 seconds to download image ? each time it's taking 17-18 seconds.

rhodanny 166 Reputation points
2021-12-26T12:47:21.547+00:00

The class Radar

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Extract
{
    class Radar
    {
        public List<string> links = new List<string>();
        string defaultlink;
        DateTime current;
        string currentLink;
        public List<DateTime> dates = new List<DateTime>();
        DateTime workingFirstDateTime;

        public List<string> folders = null;

        public Radar(List<string> inputFolders)
        {
            folders = inputFolders;

            current = RoundDown(DateTime.Now, TimeSpan.FromMinutes(-5));
        }       

        public async Task GetRadarImages()
        {
            defaultlink = "https://ims.gov.il/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;
                    GenerateRadarLinks();
                }
                catch (Exception)
                {
                    current = current.AddMinutes(-5);
                    ct = current.ToString("yyyyMMddHHmm");
                    currentLink = defaultlink + ct + ".gif";
                    await GetRadarImages();
                }
            }
        }

        private void GenerateRadarLinks()
        {
            for (var i = 0; i < 34; i++)
            {
                current = current.AddMinutes(-5);

                dates.Add(current);
                var date = dates[i].ToString("yyyyMMddHHmm");
                links.Add(defaultlink + date + ".gif");
            }

            var workingDate = workingFirstDateTime.ToString("yyyyMMddHHmm");
            dates.Insert(0, workingFirstDateTime);
        }

        DateTime RoundDown(DateTime date, TimeSpan interval)
        {
            return new DateTime(date.Ticks / interval.Ticks *
                interval.Ticks);
        }
    }
}

When it's doing the line

await wc.DownloadFileTaskAsync(new Uri(currentLink), folders[0] + "\\image0.gif");

Then it's taking 17-18 seconds to move to the next line to the catch :

current = current.AddMinutes(-5);

but why it's taking 17-18 seconds to make the DownloadFileTaskAsync ?

I form1 I have this button click event :

private void btnStart_Click(object sender, EventArgs e)
        {
            StartDownloading();
        }

Then the StartDownloading

private void StartDownloading()
        {
            btnStart.Enabled = false;
            btnRadarPath.Enabled = false;
            btnSatellitePath.Enabled = false;

            lblStatus.Text = "Preparing Downloads";
            loadingLabel1.StartCountDownTimer(1000, true);

            dates = new List<DateTime>();
            datesonimages = new List<string>();

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

            DownloadImages();
        }

And last the DownloadImages

public async void DownloadImages()
        {
            CreateDownloadFolders();

            urls = new List<string>();

            await rad.GetRadarImages();
            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();
        }
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,830 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,265 questions
{count} votes

Accepted answer
  1. AgaveJoe 26,136 Reputation points
    2021-12-26T15:17:59.693+00:00

    I ran the original code for several minutes and the only response is a 404. Regardless, your question has nothing to do with WinForms or C#. Your question is related to how https://ims.gov.il works which is out of scope for this support forum.

    Contact https://ims.gov.il support for assistance.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful