Home > Software engineering >  How can I test file download if download failed or success and keep downloading until success?
How can I test file download if download failed or success and keep downloading until success?

Time:10-16

First time when downloading the TimeDate.Now is for example 15:57 so I RoundDown make 15:55 format it and try to download it.

If no success in the completed event I'm trying to round it again from 15:55 to 15:50 formatting again and trying to download again.

The problem is that it's not rounding down again. In the completed event if there is error this line :

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

It's still the rounded 15:55 from the above and I want that until the download is not success keep round down and try to download the new currentLink with the new rounded down. 15:50 not success make it 15:45 not success 15:40 and so on round down and build the currentLink over and over again until the download is success.

public void GetImages()
        {
            defaultlink = "https://IMSRadar/IMSRadar_";

            current = RoundDown(DateTime.Now, TimeSpan.FromMinutes(-5));
            var ct = current.ToString("yyyyMMddHHmm");
            currentLink = defaultlink   ct   ".gif";

            using (System.Net.WebClient wc = new System.Net.WebClient())
            {
                wc.DownloadFileCompleted  = Wc_DownloadFileCompleted;
                wc.DownloadFileAsync(new Uri(currentLink), @"d:\test.gif");
            }
        }

        private void Wc_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                using (System.Net.WebClient wc = new System.Net.WebClient())
                {
                    current = RoundDown(current, TimeSpan.FromMinutes(-5));
                    var ct = current.ToString("yyyyMMddHHmm");
                    currentLink = defaultlink   ct   ".gif";
                    wc.DownloadFileCompleted  = Wc_DownloadFileCompleted;
                    wc.DownloadFileAsync(new Uri(currentLink), @"d:\test.gif");
                }
            }
            else
            {
                GenerateRadarLinks();
            }
        }

The method RoundDown

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

CodePudding user response:

Your roundDown won't work once you arrived at a date, which is already at some multiple of 5 minutes. Imagine the following:

Assuming integer division 103 / 5 * 5 will give 20 * 5 thus 100 as expected. But in the next iteration you then have 100 / 5 * 5 will again give 100, because 100 / 5 == 103 / 5 == 20 (with integer division).

So for this to work, you will need to decrement the time by at least one tick, so it's not a multiple of 5 minutes anymore.

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

I personally would use the RoundDown method only once to generate the first timestamp that is a multiple of 5. For all following requests, I'd just take the current timestamp and decrement it by 5 minutes.

//inital timestamp
current = RoundDown(DateTime.Now, TimeSpan.FromMinutes(-5));

//in the error handler
current = current.AddMinutes(-5);
  • Related