title: How can I download the last image each 5 minutes?
body: <p>It's a bit long but everything is connected and I wanted to explain with more details.</p>
  <p>At the constructor of form1 :</p>
  <pre><code>CreateDownloadFolders();
  </code></pre>
  <p>The method creating the directories if needed and also downloading the images once if didn't download yet :</p>
  <pre><code>private void CreateDownloadFolders()
          {
              string locationToCreateFolderRadar = textBoxRadarPath.Text;
              string locationToCreateFolderSatellite = textBoxSatellitePath.Text;
              string folderName = &quot;&quot;;
              string date = DateTime.Now.ToString(&quot;ddd MM.dd.yyyy&quot;);
              string time = DateTime.Now.ToString(&quot;HH.mm tt&quot;);
              string format = &quot;{0} On {1}&quot;;
              folderName = string.Format(format, date, time);
  
              if (!Directory.Exists(locationToCreateFolderRadar + &quot;\\&quot; + folderName))
              {
                  Directory.CreateDirectory(locationToCreateFolderRadar + &quot;\\&quot; + folderName);
              }
  
              if (!Directory.Exists(locationToCreateFolderSatellite + &quot;\\&quot; + folderName))
              {
                  Directory.CreateDirectory(locationToCreateFolderSatellite + &quot;\\&quot; + folderName);
              }
  
              if (!Directory.Exists(locationToCreateFolderRadar +
                  &quot;\\LastDownloadedImages&quot;))
              {
                  Directory.CreateDirectory(locationToCreateFolderRadar +
                      &quot;\\LastDownloadedImages&quot;);
              }
  
              radarFolderImagesDownload = locationToCreateFolderRadar + &quot;\\&quot; + folderName;
              satelliteFolderImagesDownload = locationToCreateFolderSatellite + &quot;\\&quot; + folderName;
  
              folders = new List&lt;string&gt;();
              sat = new Satellite(folders);
              rad = new Radar(folders);
  
              folders.Add(radarFolderImagesDownload);
              folders.Add(satelliteFolderImagesDownload);
  
              if(filesRadar.Length == 0 &amp;&amp; filesSatellite.Length == 0)
              {
                  StartDownloading();
              }
          }
  </code></pre>
  <p>This is how I'm downloading the images :</p>
  <p>The StartDownloading method and the DownloadImages method :</p>
  <pre><code>private void StartDownloading()
          {
              btnStart.Enabled = false;
              btnRadarPath.Enabled = false;
              btnSatellitePath.Enabled = false;
  
              lblStatus.Text = &quot;Preparing Downloads&quot;;
              loadingLabel1.StartCountDownTimer(1000, true);
  
              dates = new List&lt;DateTime&gt;();
              datesonimages = new List&lt;string&gt;();
  
              btnRadarPath.Enabled = false;
              btnSatellitePath.Enabled = false;
  
              DownloadImages();
          }
  
          public async void DownloadImages()
          {
              urls = new List&lt;string&gt;();
  
              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();
          }
  </code></pre>
  <p>And the DownloadAsync method :</p>
  <pre><code>private async Task DownloadAsync()
          {
              lblStatus.Text = &quot;Downloading&quot;;
              loadingLabel1.StartCountDownTimer(1000, false);
  
              using (var client = new WebClient())
              {
                  client.DownloadFileCompleted += (s, e) =&gt;
                  {
                      if (e.Error == null)
                      {
                          urlsCounter--;
  
                          if (downloadedImageName == satelliteFolderImagesDownload + &quot;\\satelliteImage&quot; + satCounter + &quot;.gif&quot;)
                          {
                              using (Image img = Image.FromFile(satelliteFolderImagesDownload + &quot;\\satelliteImage&quot; + satCounter + &quot;.gif&quot;))
                              {
                                  img.Save(satelliteFolderImagesDownload + &quot;\\satImage&quot; + urlsCounter + &quot;.gif&quot;, System.Drawing.Imaging.ImageFormat.Gif);
                              }
  
                              File.Delete(downloadedImageName);
                          }
  
                          if (urlsCounter == 0)
                          {
                              lblStatus.Text = &quot;Download Completed&quot;;
  
                              btnStart.Enabled = true;
                              btnRadarPath.Enabled = true;
                              btnSatellitePath.Enabled = true;
  
                              btnRadarPath.Enabled = true;
                              btnSatellitePath.Enabled = true;
  
                              radCounter = 0;
                              satCounter = 0;
  
                              dates = rad.dates;
  
                              GetImagesFiles();
  
                              EnableButtons();
  
                              Array.Sort(filesRadar, new MyComparer(false));
  
                              if (filesRadar.Length &gt; 0)
                              {
                                  for (int i = 0; i &lt; filesRadar.Length; i++)
                                  {
                                      drawOnImage.DrawText(dates[i].ToString(&quot;ddd, dd MMM yyy HH':'mm&quot;), filesRadar[i]);
                                  }
                              }
  
                              MergeImages(Image.FromFile(filesRadar[0]),
                                  Image.FromFile(filesSatellite[0])
                              , Image.FromFile(@&quot;d:\radar010.gif&quot;)).Save(@&quot;d:\Downloaded Images\merged.bmp&quot;);
  
                              CheckIfImagesExist();
  
  
                              lblLastDownloadedDate.Text = DateTime.Now.ToLongDateString();
                              lblLastDownloadedTime.Text = DateTime.Now.ToLongTimeString();
  
                              lastDownloadedDateTime = DateTime.Now;
  
                              SaveSettings();
  
                              TimeToDownload(lblNextDownloadRadar);                          
                          }
                      }
                      else
                      {
                          string error = e.Error.ToString();
                      }
                  };
  
                  client.DownloadProgressChanged += (s, e) =&gt; tracker.SetProgress(e.BytesReceived, e.TotalBytesToReceive);
                  client.DownloadProgressChanged += (s, e) =&gt; lblAmount.Text = tracker.SizeSuffix(e.BytesReceived) + &quot;/&quot; + tracker.SizeSuffix(e.TotalBytesToReceive);
                  client.DownloadProgressChanged += (s, e) =&gt; lblSpeed.Text = tracker.GetBytesPerSecondString();
                  client.DownloadProgressChanged += (s, e) =&gt; myLong = Convert.ToInt64(client.ResponseHeaders[&quot;Content-Length&quot;]);
                  client.DownloadProgressChanged += (s, e) =&gt;
                  {
                      progressBar1.Value = e.ProgressPercentage;
                      label1.Text = e.ProgressPercentage + &quot;%&quot;;
                  };
  
                  for (int i = 0; i &lt; urls.Count; i++)
                  {
                      tracker.NewFile();
  
                      if (urls[i].Contains(&quot;Radar&quot;))
                      {
                          lblStatus.Text = &quot;Downloading Radar Images&quot;;
  
                          downloadedImageName = radarFolderImagesDownload + &quot;\\radarImage&quot; + radCounter + &quot;.gif&quot;;
  
                          radCounter++;
                          try
                          {
                              await client.DownloadFileTaskAsync(new Uri(urls[i]), radarFolderImagesDownload + &quot;\\radarImage&quot; + radCounter + &quot;.gif&quot;);
                          }
                          catch (WebException ex)
                          {
                              Logger.Write($&quot;Downloading {urls[i]} failed. {ex.Message}&quot;);
                              throw;
                          }
                          catch (InvalidOperationException)
                          {
                              Logger.Write($&quot;Saving {urls[i]} to {radarFolderImagesDownload + &quot;\\radarImage&quot; + radCounter + &quot;.gif&quot;} failed. File is in use.&quot;);
                              throw;
                          }
                      }
                      else
                      {
                          lblStatus.Text = &quot;Downloading Satellite Images&quot;;
  
                          downloadedImageName = satelliteFolderImagesDownload + &quot;\\satelliteImage&quot; + satCounter + &quot;.gif&quot;;
  
                          await client.DownloadFileTaskAsync(new Uri(urls[i]), satelliteFolderImagesDownload + &quot;\\satelliteImage&quot; + satCounter + &quot;.gif&quot;);
  
                          satCounter++;
                      }
                  }
              }
          }
  </code></pre>
  <p>Last the Radar class :</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace Extract
  {
      class Radar
      {
          public List&lt;string&gt; links = new List&lt;string&gt;();
          string defaultlink;
          DateTime current;
          string currentLink;
          public List&lt;DateTime&gt; dates = new List&lt;DateTime&gt;();
          DateTime workingFirstDateTime;
  
          public List&lt;string&gt; folders = null;
  
          public Radar(List&lt;string&gt; inputFolders)
          {
              folders = inputFolders;
  
              current = RoundDown(DateTime.Now, TimeSpan.FromMinutes(-5));
          }       
  
          public async Task GetRadarImages()
          {
              defaultlink = &quot;https://ims.gov.il/sites/default/files/ims_data/map_images/IMSRadar/IMSRadar_&quot;;
              
              var ct = current.ToString(&quot;yyyyMMddHHmm&quot;);
              currentLink = defaultlink + ct + &quot;.gif&quot;;
  
              using (System.Net.WebClient wc = new System.Net.WebClient())
              {
                  try
                  {
                      await wc.DownloadFileTaskAsync(new Uri(currentLink), folders[0] + &quot;\\image0.gif&quot;);
                      workingFirstDateTime = current;
                      GenerateRadarLinks();
                  }
                  catch (Exception)
                  {
                      current = current.AddMinutes(-5);
                      ct = current.ToString(&quot;yyyyMMddHHmm&quot;);
                      currentLink = defaultlink + ct + &quot;.gif&quot;;
                      await GetRadarImages();
                  }
              }
          }
  
          private void GenerateRadarLinks()
          {
              for (var i = 0; i &lt; 34; i++)
              {
                  current = current.AddMinutes(-5);
  
                  dates.Add(current);
                  var date = dates[i].ToString(&quot;yyyyMMddHHmm&quot;);
                  links.Add(defaultlink + date + &quot;.gif&quot;);
              }
  
              var workingDate = workingFirstDateTime.ToString(&quot;yyyyMMddHHmm&quot;);
              //links.Insert(0, defaultlink + workingDate + &quot;.gif&quot;);
              dates.Insert(0, workingFirstDateTime);
          }
  
          DateTime RoundDown(DateTime date, TimeSpan interval)
          {
              return new DateTime(date.Ticks / interval.Ticks *
                  interval.Ticks);
          }
  
          public async Task DownloadLastImage()
          {
              defaultlink = &quot;https://ims.gov.il/sites/default/files/ims_data/map_images/IMSRadar/IMSRadar_&quot;;
  
              var ct = current.ToString(&quot;yyyyMMddHHmm&quot;);
              currentLink = defaultlink + ct + &quot;.gif&quot;;
  
              using (System.Net.WebClient wc = new System.Net.WebClient())
              {
                  try
                  {
                      await wc.DownloadFileTaskAsync(new Uri(currentLink), folders[0] + &quot;\\image0.gif&quot;);
                  }
                  catch (Exception)
                  {
                      current = current.AddMinutes(-5);
                      ct = current.ToString(&quot;yyyyMMddHHmm&quot;);
                      currentLink = defaultlink + ct + &quot;.gif&quot;;
                      await GetRadarImages();
                  }
              }
          }
      }
  }
  </code></pre>
  <p>The method GetRadarImages generate all the link for the first time downloading in the form1 constructor.</p>
  <p>The method DownloadLastImage should download each time only a single image the last exist image every 5 minutes.</p>
  <p>In the DownloadAsync method when all the images have been downloaded I'm starting a timer counting down 5 minutes :</p>
  <pre><code>TimeToDownload(lblNextDownloadRadar);
  
  private void TimeToDownload(Label LabelToDisplay)
          {
              TimeSpan time1 = TimeSpan.FromSeconds(_elapsedSeconds);
              LabelToDisplay.Text = time1.ToString(@&quot;hh\:mm\:ss&quot;);
  
              System.Windows.Forms.Timer _timer = new System.Windows.Forms.Timer
              {
                  Interval = 1000,
                  Enabled = true
              };
              _timer.Tick += (sender, args) =&gt;
              {
                  _elapsedSeconds--;
                  TimeSpan time = TimeSpan.FromSeconds(_elapsedSeconds);
                  LabelToDisplay.Text = time.ToString(@&quot;hh\:mm\:ss&quot;);
  
                  if(time.Hours == 0 &amp;&amp; time.Minutes == 0 &amp;&amp; time.Seconds == 0)
                  {
                      // Here to download the last single image.
  
                      _elapsedSeconds = 300;
                  }
              };
          }
  </code></pre>
  <p>My problem is how to download the last image from the radar class with the method public async Task DownloadLastImage() but that it will show the download progress and all the info in form1 DownloadAsync method the same when downloading all the images first time ?</p>